Unsolved run function using thread
-
I want to use Thread I run the following code: pls help me :((
void MyClass::login(QString usr, QString psw) { QSqlDatabase db = QSqlDatabase::addDatabase( "QMYSQL" ); db.setHostName( "localhost" ); db.setDatabaseName( "dbkanontablighati" ); db.setUserName( "root" ); db.setPassword( "" ); db.open(); if( db.open() ) { QSqlQuery qry; if(qry.exec("select * from tbl_user where UserName='"+usr+"', Password='"+psw+"'")) { qDebug() << "True"; } else { qDebug() << qry.lastError();; } } else { qDebug() << db.lastError(); } }
-
Hello,
I have one question and one warning.Question: Why do you feel you need to run this code in a thread?
Warning: Neither
QSqlDatabase
norQSqlQuery
are reentrant, so even if you run it in a thread you have to be extra careful.Kind regards.
-
Hi My Bro Sorry. im very beginner :D , Because when I want to do an operation a few seconds after the button was Hong App
I am beginner please guide me :( -
@MrErfan
Hello,
It is possible to make this run in a thread, but you should ensure that all SQL operations run in that same thread! Here you could find a decent explanation how you can use thread with a workerQObject
, this is the low-level approach, which I believe is appropriate in your case. Basically, you create your own class by extendingQObject
, start a thread (createQThread
instance and callQThread::start
) and then control the worker and the thread via signal-slot connections. You can push or pull information from your worker object by defining and emitting signals where appropriate. If you're not well versed how signals and slots work, then you should read this part of the documentation. I hope this helps.Kind regards.