Problems with QtSqlDatabase and threads



  • Hi, i have an application that use threads and (if possible) a Sqlite database.
    the main is this one:

    @int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.showMaximized();

    QMutex *configurationMutex=new QMutex();
    Configuration *configuration=new Configuration();

    Checker checker(0,configuration,configurationMutex);
    Networker networker(0,configuration,configurationMutex);
    Executer executer(0,configuration,configurationMutex);

    QThread checkerThread;
    QThread networkerThread;
    QThread executerThread;

    QObject::connect(&executerThread,SIGNAL(started()),&executer,SLOT(Start()));
    QObject::connect(&w,SIGNAL(SignalTest()),&checker,SLOT(TestSlot()));

    checker.moveToThread(&checkerThread);
    networker.moveToThread(&networkerThread);
    executer.moveToThread(&executerThread);

    file_name();

    checkerThread.start();
    networkerThread.start();
    executerThread.start();

    int returnValue=a.exec();

    executerThread.wait();

    return returnValue;

    }@

    in the executer thread start function i have this:

    @QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("testDB.db3");
    if(db.open())
    {
    qDebug()<<"Database opened";
    db.close();
    }
    else
    {
    qDebug()<<"Problem opening database "<<db.lastError().text();
    }@

    but driver load failed and it gives me those errors:

    @
    QCoreApplication::applicationDirPath: Please instantiate the QApplication object first
    SqlDatabase: QSQLITE driver not loaded
    QSqlDatabase: available drivers:
    QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins
    @

    i think is because i start the executer thread before calling a.exec()..
    How can i solve this?

    thanks,
    Riccardo


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.