Guest User

Untitled

a guest
Jun 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. /* include some QT and ANSI C library headers. */
  2. #include <QtGui>
  3. #include <QtSql>
  4.  
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. /* include headers defining the main form. */
  10. #include "mainform.h"
  11.  
  12. /* declare DB driver and filename. */
  13. static const QString dbDriverStr = "QSQLITE";
  14. static const QString dbFileNameStr = "database.db";
  15.  
  16. /* GUI string messages. */
  17. static const QString dbConnectErrorStr = QObject::tr("Database Connection Error");
  18. static const QString dbDriverNotExistStr = QObject::tr("Database driver is not available.");
  19. static const QString dbCannotOpenStr = QObject::tr("The database cannot open.");
  20.  
  21. static bool
  22. createDBConnection () {
  23. /* check the existence of the DB driver. */
  24. if (!QSqlDatabase::isDriverAvailable (dbDriverStr)) {
  25. QMessageBox::critical(0, dbConnectErrorStr, dbDriverNotExistStr);
  26. /* DB connection failed. */
  27. return false;
  28. }
  29.  
  30. /* connect to the DB with the following driver. */
  31. QSqlDatabase db = QSqlDatabase::addDatabase(dbDriverStr);
  32.  
  33. /* use the following database name. */
  34. db.setDatabaseName(dbFileNameStr);
  35.  
  36. /* if the DB cannot open for any reason print a warning. */
  37. if (!db.open()) {
  38. QMessageBox::critical(0, dbConnectErrorStr, dbCannotOpenStr);
  39.  
  40. /* DB connection failed. */
  41. return false;
  42. }
  43.  
  44. /* DB connection successed. */
  45. return true;
  46. }
  47.  
  48. /* main function. */
  49. int
  50. main(int argc, char *argv[]) {
  51. /* create the application. */
  52. QApplication app(argc, argv);
  53.  
  54. /* try to connect to DB. */
  55. if (!createDBConnection ()) {
  56. return EXIT_FAILURE;
  57. }
  58.  
  59. /* create the main form of the app. */
  60. MainForm form;
  61.  
  62. /* show the main form. */
  63. form.show();
  64.  
  65. /* run the application. */
  66. return app.exec();
  67. }
Add Comment
Please, Sign In to add comment