Planned maintenance: From Sunday 8th December 10:00 CET there will be changes to try and solve the caching issues that have been experienced. If anyone has a problem connecting after this period then please PM @AndyS or any of the moderators.

Qt suddenly claims it can't find QSqlDatabase



  • Hi all.

    I've decided to turn the core portion of my app into a library. I've made appropriate changes to the project file & added the Q_DECL_EXPORT macros.

    At some point (I have no idea what triggered it), compilation started failing on

    #include <QSqlDatabase>
    

    I have

    QT       += core bluetooth sql
    

    in the project file (where it always was) and haven't made any changes to my Qt installation. If I start typing that include line, it auto-completes "QSqlDatabase." Anybody have an idea here? Thanks!


  • Qt Champions 2018

    @Stokestack please re-run qmake and then rebuild the project.

    If the error persists, post the compiler command line leading to this error.

    Regards



  • @aha_1980 Thanks for the response.

    For once, I had remembered to run qmake (and clean) and it didn't help.

    Here's the output. It's possible I haven't set this thing up to be a static lib correctly, but I don't know what might be wrong:

    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -stdlib=libc++ -g -std=gnu++1z  -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -mmacosx-version-min=10.12 -Wall -W -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I../../AtomRemote-all/AtomRemote-mobile -I. -I../../AtomRemote-all/AtomRemote -I/Developer/Qt/5.12.3/clang_64/lib/QtQuick.framework/Headers -I/Developer/Qt/5.12.3/clang_64/lib/QtGui.framework/Headers -I/Developer/Qt/5.12.3/clang_64/lib/QtQml.framework/Headers -I/Developer/Qt/5.12.3/clang_64/lib/QtNetwork.framework/Headers -I/Developer/Qt/5.12.3/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode10.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Applications/Xcode10.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/AGL.framework/Headers/ -I/Developer/Qt/5.12.3/clang_64/mkspecs/macx-clang -F/Developer/Qt/5.12.3/clang_64/lib -o main.o ../../AtomRemote-all/AtomRemote-mobile/main.cpp
    /Developer/Qt/5.12.3/clang_64/bin/rcc -name qml ../../AtomRemote-all/AtomRemote-mobile/qml.qrc -o qrc_qml.cpp
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -stdlib=libc++ -g -std=gnu++1z  -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -mmacosx-version-min=10.12 -Wall -W -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_QUICK_LIB -DQT_GUI_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -I../../AtomRemote-all/AtomRemote-mobile -I. -I../../AtomRemote-all/AtomRemote -I/Developer/Qt/5.12.3/clang_64/lib/QtQuick.framework/Headers -I/Developer/Qt/5.12.3/clang_64/lib/QtGui.framework/Headers -I/Developer/Qt/5.12.3/clang_64/lib/QtQml.framework/Headers -I/Developer/Qt/5.12.3/clang_64/lib/QtNetwork.framework/Headers -I/Developer/Qt/5.12.3/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode10.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Applications/Xcode10.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/AGL.framework/Headers/ -I/Developer/Qt/5.12.3/clang_64/mkspecs/macx-clang -F/Developer/Qt/5.12.3/clang_64/lib -o qrc_qml.o qrc_qml.cpp
    In file included from ../../AtomRemote-all/AtomRemote-mobile/main.cpp:5:
    In file included from ../../AtomRemote-all/AtomRemote/DBManager.h:10:
    ../../AtomRemote-all/AtomRemote/AtomSystemDAO.h:4:10: fatal error: 'QSqlDatabase' file not found
    #include <QSqlDatabase>
    

    ![alt text](0_1561276403428_projectStructure.png image url)


  • Qt Champions 2018

    @Stokestack Well, I don't see an include path for QtSql in your compiler call. Are you sure you added QT += SQL to the correct .pro file?

    Because it seems you have a SUBDIRS project with multiple .pro files.



  • Thanks! Well, it's in the .pro file that builds the lib that uses SQL.

    But, sure enough, also adding it to the .pro file that will link with the library eliminates the error. All I do in that project so far is instantiate one of the objects from the lib.

    So a Qt project that uses a lib must also add all of the modules that the lib uses to its own project file?


  • Qt Champions 2018

    @Stokestack said in Qt suddenly claims it can't find QSqlDatabase:

    So a Qt project that uses a lib must also add all of the modules that the lib uses to its own project file?

    No. Every lib is on its own. You can have a lib that uses QtSql, and an app that just uses this lib. But if you use QtSql directly in your app, you have to add the module there too.



  • @aha_1980 Well, this is a bit perplexing then. All I do in this app is

    	DBManager& DBMgr = DBManager::getInstance();
    
    

    There is no further use of anything from the lib, although I do say this:

    	QQmlContext* pContext = engine.rootContext();
    	pContext->setContextProperty("DBMgr", &DBMgr);
    

    Does exposing the whole object to the "client" project somehow trigger this requirement?


  • Qt Champions 2018

    @Stokestack

    Does exposing the whole object to the "client" project somehow trigger this requirement?

    No, now I've got it:

    In file included from ../../AtomRemote-all/AtomRemote-mobile/main.cpp:5:
    In file included from ../../AtomRemote-all/AtomRemote/DBManager.h:10:
    ../../AtomRemote-all/AtomRemote/AtomSystemDAO.h:4:10: fatal error: 'QSqlDatabase' file not found

    You just need to read the error message carefully.

    You include QSqlDatabase in your DBManager.h, which in turn is included from your app project. Therefore you get the need to find the Qt database classes in your include path.

    Refactor your lib to get rid of that. E.g. you can use the following forward declaration in your header file:

    #include <QObject>
    
    QT_BEGIN_NAMESPACE
    class QSqlDatabase;
    QT_END_NAMESPACE
    
    class DBManager {
    
     // ...
    
    private:
      QSqlDatabase *m_db = nullptr;
    }
    

    and only include QSqlDatabase in the corresponding DBManager.cpp file.

    Regards



  • Duh! Thanks for pointing that out!


  • Lifetime Qt Champion

    Hi,

    One thing to take into account: there's no need to store any local QSqlDatabase variable (or pointer). This class already manages all connections for you.



  • This post is deleted!


  • @SGaist Just came back to say to say thanks again for thinking to add this remark. I was indeed pointlessly storing the DB connection in member variables and complicating my task.


Log in to reply