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. executing query after cloned connection is used in another thread
Forum Updated to NodeBB v4.3 + New Features

executing query after cloned connection is used in another thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
26 Posts 3 Posters 4.0k Views 2 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.
  • Christian EhrlicherC Christian Ehrlicher

    Your function queryExecutor::execute() is not run in your thread since the slot from where it's called is a function from an object which lives in the main thread. I would suggest reading the QThread documentation on how to properly use Signals/Slots with QThread: https://doc.qt.io/qt-5/threads-qobject.html

    U Offline
    U Offline
    user4592357
    wrote on last edited by user4592357
    #21

    @christian-ehrlicher
    yeah that's right. query executor should execute the query since it is in the new thread, so the code should be like this:

    void QueryThread::execute(const QString &query, const QHash<QString, QString> &mapBoundVals)
    {
    	// forward the execution to the worker
    	emit executefwd(query, mapBoundVals);
    }
    
    void QueryThread::run()
    {
    	qDebug() << "started thread" << QThread::currentThreadId();
    
    	m_queryExecutor.setDatabase(m_sDbPath);
    
            connect( this, SIGNAL( executefwd( const QString&, const QHash<QString, QString> & ) ),
                 m_queryExecutor, SLOT( execute( const QString&, const QHash<QString, QString> & ) ) );
    
    	// forward final signal
    	connect(&m_queryExecutor, &queryExecutor::finished, this, &QueryThread::queryFinished);
    
    	exec();
    }
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #22

      Your executor still lives in the main thread as it is created at your custom thread subclass creation time.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      U 1 Reply Last reply
      1
      • SGaistS SGaist

        Your executor still lives in the main thread as it is created at your custom thread subclass creation time.

        U Offline
        U Offline
        user4592357
        wrote on last edited by
        #23

        @sgaist
        oh okay, so it should be newed in QueryThread::run()

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #24

          It can be on the stack. It really depends on how you want to implement that stuff. You seem to be mixing the worker object approach with the subclass QThread approach.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          U 1 Reply Last reply
          0
          • SGaistS SGaist

            It can be on the stack. It really depends on how you want to implement that stuff. You seem to be mixing the worker object approach with the subclass QThread approach.

            U Offline
            U Offline
            user4592357
            wrote on last edited by
            #25

            @sgaist
            i followed this example for my implementation: https://www.linuxjournal.com/article/9602
            but anyways i think thread is still not much helpful because my for loop still executes in main thread. am i right?

            bool doWork(const QString &newVal)
            {
                 for (each of selected table rows)
                 {
                      if (!table->updateRow(row, newVal))
                          return false;
                 }
                return true;
            }
            
            bool Table::updateRow(int row, const QString &newVal)
            {
                 //update in another thread
            }
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #26

              As already said, you are mixing two different technique the wrong way.

              If you subclass QThread and re-implement run, then what is executed in run happens in the thread managed by QThread.

              However, you created an instance of your worker object in the original thread since it's part of your QThread subclass and thus the slot will be executed in the original thread.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

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