Solved Will compile / Won't compile
-
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?
-
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 ;)
-
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
Hehe. beaten by @mpergand by a second :) \o/
-
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!
-
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.