PDA

View Full Version : QtSql and Thread



Ieshir
17th September 2014, 19:38
Hello.
1)I have a class (doesn't work with gui), where I have slot for connect to the database(PSQL)

MyClass * cl = new MyClass;
2)next i create new thread

QThread * thread = new QThread;
myclass->moveToThread(thread);
thread->start();
..
emit connectToDB();
...


3)next make query in gui-class or myclass;

QSqlQuery query;
query.prepare("text here");
query.exec(); //
4)

Threads and the SQL Module

A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.

why query.exec() return true with correct data in gui-thread and another thread?

ChrisW67
17th September 2014, 22:02
QSqlQuery::exec() returns true if the query executed. There is insufficient information in your post to know how and where the connection was created and thus whether this is expected, correct, or accidentally correct behaviour.

Ieshir
18th September 2014, 03:58
What other information should be submitted?

wysota
20th September 2014, 22:31
What other information should be submitted?

At least show us where you create database connections. If what you posted is an actual code then it is most likely invalid as the query uses the default database connection.