Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. run function using thread
Forum Updated to NodeBB v4.3 + New Features

run function using thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 933 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MrErfanM Offline
    MrErfanM Offline
    MrErfan
    wrote on last edited by
    #1

    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();
    }
    
    }
    
    1 Reply Last reply
    0
    • kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • MrErfanM Offline
        MrErfanM Offline
        MrErfan
        wrote on last edited by MrErfan
        #3

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

        kshegunovK 1 Reply Last reply
        0
        • MrErfanM MrErfan

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

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @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.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved