Results 1 to 4 of 4

Thread: QT doesn't recognise <QtSql> header files

  1. #1
    Join Date
    Jun 2014
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default QT doesn't recognise <QtSql> header files

    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"
    Qt Code:
    1. if (!QSqlDatabase::drivers().contains("QSQLITE"))
    2. QMessageBox::critical(this, "Unable to load database", "This demo needs the SQLITE driver");
    3.  
    4. // initialize the database
    5. QSqlError err = initDb();
    6. if (err.type() != QSqlError::NoError) {
    7. showError(err);
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by anda_skoa; 12th June 2014 at 15:23. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT doesn't recognise <QtSql> header files

    Have you added the SQL module to your .pro file's QT configuration?
    Qt Code:
    1. QT += sql
    To copy to clipboard, switch view to plain text mode 

    Btw, what you usually actually want is an include like this
    Qt Code:
    1. #include <QSqlDatabase>
    To copy to clipboard, switch view to plain text mode 
    i.e. the classes you actually need

    Cheers,
    _

  3. #3
    Join Date
    Jun 2014
    Posts
    6
    Qt products
    Qt5
    Platforms
    Unix/X11

    Smile Re: QT doesn't recognise <QtSql> header files

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT doesn't recognise <QtSql> header files

    No idea, I've always used the manual approach since this works regardless of which IDE is being used for a given project

    Cheers,
    _

Similar Threads

  1. Header Files
    By Maluko_Da_Tola in forum Newbie
    Replies: 1
    Last Post: 2nd October 2010, 12:04
  2. Replies: 2
    Last Post: 30th September 2010, 12:26
  3. The header file 'mainwindow.h' doesn't include <QObject>.
    By nhs_0702 in forum Qt Programming
    Replies: 5
    Last Post: 14th May 2010, 17:02
  4. Qt Eclipse can not recognise some type and function!why?
    By greenoaktree in forum Qt Programming
    Replies: 2
    Last Post: 21st January 2008, 06:51
  5. header files not getting included
    By nimmyj in forum Installation and Deployment
    Replies: 1
    Last Post: 19th December 2006, 06:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.