Important: Please read the Qt Code of Conduct -

QPSQL Multithread Crash

  • Hello,

    Whats wrong with running this code in a thread:

    @ QSqlDatabase db = QSqlDatabase::addDatabase( "QPSQL", QString::number( (long long)(QThread::currentThread())) );
    db.setHostName( QString::fromStdString( m_Hostname ) );
    db.setPort( m_Port );
    db.setDatabaseName(QString::fromStdString( m_DatabaseName ));
    db.setUserName(QString::fromStdString( m_User ));
    db.setPassword(QString::fromStdString( m_PW ));

    bool online =;
    if(!online) {
    m_HasError = true;
    return DATA_CANCELLED;

    QSqlQuery query(QString::fromStdString( m_CountMeltingsQuery ), db);@

    I mwan this runs, but when i close the MainWindow... The program crashes at:

    qsqlpsqld4.dll!QPSQLDriver::close() Zeile 848 C++ <--- Crash!
    QtSqld4.dll!QSqlDatabase::close() Zeile 882 C++

    The code in qpsqldriver is:
    @ if (d->connection)
    d->connection = 0;@

  • Did you read "Threads and the SQL Module": of the QSqlDatabase documentation?

    Did you open another connection in another thread?
    Are you sure the code is running in the new thread?

Log in to reply