Question

I would like to do a SQL on a separated thread in order to avoid blocking the GUI thread. As QSqlQuery has to be created in the thread that runs the QSqlQuery::exec methods, I have created a slot which returns a QSqlQuery object and connect it with a Qt::BlockingQueuedConnection signal. However, it reports

QObject::connect: Cannot queue arguments of type 'QSqlQuery&' (Make sure 'QSqlQuery&' is registered using qRegisterMetaType().)

while running.

However, registering "QSqlQuery&" results in

error C2770: invalid explicit template argument(s) for 'int qRegisterMetaType(T *)'

in Visual C++ 8.0 while registering "QSqlQuery" work fine. Please help me creating a QSqlQuery for prepare and bindValue on another thread.

Many Thanks!

Here are my code snip.

class Handler
    : public QObject
{
    Q_OBJECT

...

public slots:

    void onGetQuery
        ( QSqlQuery& orQuery
        )
    {
        orQuery = QSqlQuery(mrDb);
    }

    void onExec
        ( QSqlQuery irQuery
        )
    {
        irQuery.exec();
        fireReady(irQuery);
    }

protected:

    QSqlDatabase mrDb;

    ...

signals:

    void fireReady
        ( QSqlQuery irQuery
        );

};


class Db
    : public QObject
{
    Q_OBJECT
...

public:

    Db
        ( Handler* ipHandler
        )
        : QObject(0)
    {
        connect(
            this,
            SIGNAL(fireGetQuery(QSqlQuery&)),
            ipHandler,
            SLOT(onGetQuery(QSqlQuery&)),
            Qt::BlockingQueuedConnection);
    }


    void getQuery
        ( QSqlQuery& orQuery
        )
    {
        fireGetQuery(orQuery);
    }

...

signals:

    void fireGetQuery
        ( QSqlQuery& orQuery
        );
};


int main
    ( int inArgc
    , char* ipArgv[]
    )
{
    QCoreApplication lrApp(inArgc, ipArgv);
...
    Db lrDb(lpHandler);
    QSqlQuery lrQuery;
    lrDb.getQuery(lrQuery);
    lrQuery.prepare(...);
    lrQuery.bindValue(...);
...
}
Was it helpful?

Solution

I have found out the answer.

QSqlQuery::prepare and QSqlQuery::bindValue methods cannot be called outside the thread created them. Hence there is no reason to pass it around in signal-slot.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top