Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Will compile / Won't compile
Forum Updated to NodeBB v4.3 + New Features

Will compile / Won't compile

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 817 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • bart.hollisB Offline
    bart.hollisB Offline
    bart.hollis
    wrote on last edited by
    #1

    After asking whether or not to include a test for database drivers, and being told I should, I went to my original project, based on some of the sample code, which has the recommended test, copied the test and pasted it into the current version of my program. The new version will not compile and in fact, will not even recognize the existence of the include file.

    Here is the code from the old version that does compile:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "initdb.h"
    #include <QtSql>
    #include <QMessageBox>
    #include <QModelIndex>
    #include <QSqlRelationalTableModel>
    #include <iostream>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        // Ensure that we have the proper drivers loaded.
        if (!QSqlDatabase::drivers().contains("QSQLITE"))
        {
            QMessageBox::critical(this, "Unable to load database", "This program needs the SQLITE driver");
            return;
        }
    }
    
    
    

    Notice the #include <QtSql> line

    Here's the code from the new version:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QTimer>
    #include <QMessageBox>
    
    #include <QModelIndex>
    
    //#include <QtSql>
    
    #include <QtSql/qsql.h>
    #include <QtSql/qsqldatabase.h>
    #include <QtSql/qsqldriver.h>
    #include <QtSql/qsqldriverplugin.h>
    #include <QtSql/qsqlerror.h>
    #include <QtSql/qsqlfield.h>
    #include <QtSql/qsqlindex.h>
    #include <QtSql/qsqlquery.h>
    #include <QtSql/qsqlquerymodel.h>
    #include <QtSql/qsqlrecord.h>
    #include <QtSql/qsqlrelationaldelegate.h>
    #include <QtSql/qsqlrelationaltablemodel.h>
    #include <QtSql/qsqlresult.h>
    #include <QtSql/qsqltablemodel.h>
    #include <QtSql/qtsqlversion.h>
    
    // to be removed!
    #include <iostream>
    
    
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        QTimer::singleShot(0, this, SLOT(checkDataBase()));
    
    
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::checkDataBase()
    {
    
        /* ************************************************************** */
        /* This is where we need the logic that will create the database, */
        /* create the tables, check for data, and allow sample data to be */
        /* inserted if the user wants.                                    */
        /* ************************************************************** */
    
    
        if (QSqlDatabase::drivers().contains("QSQLITE"))
        {
            QMessageBox::critical(this, "Unable to load database", "This program needs the SQLITE driver");
            return;
        }
    
    
    

    Notice the #include <QtSql> is commented out. So, I went to /use/include/qt5/QtSql and found it is a directory. So I add every .h file as an include. I now get an error "undefined reference to 'QSqlDatabase::drivers()'

    I've come to the conclusion I need help with this one. Why does one project recognize the QtSqL folder as an include yet the other won't?

    And Why is QSqlDatabase::drivers() undefined in the new project and not in the old one?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by
      #2

      If I take a look at the doc of QtSql, I see:

      To include the definitions of the module's classes, use the following directive:
      
        #include <QtSql>
      
      To link against the module, add this line to your qmake .pro file:
      
        QT += sql
      

      Seems you missed the last line ;)

      1 Reply Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        Some of the classes in Qt lives in its own module.
        To enable such module, you need to add something to the .pro file.
        In this case , it needs
        QT += sql
        To know the Sql Module and hence its includes.

        So make sure yo have that in .pro file. if not and you add it,
        make sure to run qmake from the menu after.

        Other example
        alt text

        Hehe. beaten by @mpergand by a second :) \o/

        1 Reply Last reply
        4
        • bart.hollisB Offline
          bart.hollisB Offline
          bart.hollis
          wrote on last edited by
          #4

          Well, that was easy! For you guys! That did indeed make the difference.
          Thanks for your quick response!

          And, I'll be coming to the printer before I'm done. Count on me remembering this!

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            Just a recommandation: don't use module wide includes unless you are building a small test application. These are pulling in all the includes available in the module which means that your compile time will go up for not much benefit. Only include what you actually use. That will also make things clearer when reading your code.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved