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();
    }
    
    }
    

  • Qt Champions 2016

    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 nor QSqlQuery 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 :(


  • Qt Champions 2016

    @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 worker QObject, this is the low-level approach, which I believe is appropriate in your case. Basically, you create your own class by extending QObject, start a thread (create QThread instance and call QThread::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.


Log in to reply
 

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