Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* include some QT and ANSI C library headers. */
- #include <QtGui>
- #include <QtSql>
- #include <cstdlib>
- using namespace std;
- /* include headers defining the main form. */
- #include "mainform.h"
- /* declare DB driver and filename. */
- static const QString dbDriverStr = "QSQLITE";
- static const QString dbFileNameStr = "database.db";
- /* GUI string messages. */
- static const QString dbConnectErrorStr = QObject::tr("Database Connection Error");
- static const QString dbDriverNotExistStr = QObject::tr("Database driver is not available.");
- static const QString dbCannotOpenStr = QObject::tr("The database cannot open.");
- static bool
- createDBConnection () {
- /* check the existence of the DB driver. */
- if (!QSqlDatabase::isDriverAvailable (dbDriverStr)) {
- QMessageBox::critical(0, dbConnectErrorStr, dbDriverNotExistStr);
- /* DB connection failed. */
- return false;
- }
- /* connect to the DB with the following driver. */
- QSqlDatabase db = QSqlDatabase::addDatabase(dbDriverStr);
- /* use the following database name. */
- db.setDatabaseName(dbFileNameStr);
- /* if the DB cannot open for any reason print a warning. */
- if (!db.open()) {
- QMessageBox::critical(0, dbConnectErrorStr, dbCannotOpenStr);
- /* DB connection failed. */
- return false;
- }
- /* DB connection successed. */
- return true;
- }
- /* main function. */
- int
- main(int argc, char *argv[]) {
- /* create the application. */
- QApplication app(argc, argv);
- /* try to connect to DB. */
- if (!createDBConnection ()) {
- return EXIT_FAILURE;
- }
- /* create the main form of the app. */
- MainForm form;
- /* show the main form. */
- form.show();
- /* run the application. */
- return app.exec();
- }
Add Comment
Please, Sign In to add comment