PDA

View Full Version : QT doesn't recognise <QtSql> header files



johnL
12th June 2014, 14:59
I'm new to QT and am trying to develop a database application. To understand step by step, I am Cutting and pasting code from the books example.

The following code from "bookwindow.cpp"


if (!QSqlDatabase::drivers().contains("QSQLITE"))
QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver");

// initialize the database
QSqlError err = initDb();
if (err.type() != QSqlError::NoError) {
showError(err);

}
compiles and works fine in books example

BUT when copied into mainwindow.cpp for a new QtWidget project compilation fails with message "undefined reference to `QSqlDatabase::drivers()"

When adding the import line ...#include <QtSql>... the include line is highlighted as an error. Tool tip displays "QtSql: No such file or Directory"

However, when typing, QT Creator actually recognises the text and proposes text completion of <QtSql/ and allows selection of <QtSql/QtSql>. Hovering over this gives a tool tip "/home/john/Qt5.2.1/5.2.1/gcc_64/include/QtSql/QtSql"
In contrast, the books example includes the line ...#include <QtSql>... which displays the expected tooltip "/home/john/Qt5.2.1/5.2.1/gcc_64/include/QtSql/QtSql".

Despite QT obviously locating the header files (it proposes text completion) it appears that QT Creator does NOT include the file on compilation, giving rise to the error.

I've looked through the Project files and there do not appear to be any editable project settings for header file directories or any other obvious problem with my code.
Also, entering similar text eg ...#include <QtWidgets>... works correctly, including the header file and QT ackowledges the inclusion in tooltips

Why does Qt not appear to recognise the #include <QtSql> directive? How do I get QT to consistently include headers from the normal header file directories?

Regards

anda_skoa
12th June 2014, 15:25
Have you added the SQL module to your .pro file's QT configuration?


QT += sql


Btw, what you usually actually want is an include like this


#include <QSqlDatabase>

i.e. the classes you actually need

Cheers,
_

johnL
13th June 2014, 00:57
Thanks very much - that was it.

I have used CBuilder and Netbeans IDE's in the past and both these have an "add library" action accessible through the main menus.
Is there a menu item / dialog for adding libraries in QT or is it only done by text editing the project file? (Admittedly, not to hard once you know the trick)

Regards
johnL

anda_skoa
13th June 2014, 09:25
No idea, I've always used the manual approach since this works regardless of which IDE is being used for a given project :)

Cheers,
_