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. problems with Qt::QueuedConnection

problems with Qt::QueuedConnection

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.5k 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.
  • R Offline
    R Offline
    Revi
    wrote on last edited by Chris Kawa
    #1

    Hi,
    I am pretty sure it is an easy error or a general misunderstanding but I don't see it so I am posting here.

    I have a MainWindow class which holds a Sync object and a Logger object.

    After a action is triggered a function in the mainwindow starts a function from the sync object in another thread. (std::thread).
    e.g.: std::thread a(&Sync::startSyncProcess,sync_.get());
    The thread is immediately detached.

    The sync Objects holds a pointer to a log object which holds pointers to all objects that needs information about the sync process.
    The "Logger" is a QObject with a Q_Object macro.
    The sync objects calls a Logger::report(int step) function which emits a Logger::report_progress_to_mwnd(step) function.

    In the Constructor of the Logger I made a Qt::QueuedConnection between the Logger::report and Logger::reports_progress_to_mwnd function.

    Here is the basic code:

    class Logger : public QObject
    {
    	Q_OBJECT
    public:
    	Logger(QProgressBar * mwpb) : mw_pb_(mwpb)
    	{
    		QObject::connect(this, SIGNAL(report(int)), this, SLOT(report_progress_to_mwnd(int)), Qt::QueuedConnection);
    	}
    	void report(int step){ 
    		emit(report_progress_to_mwnd(step)); 
    	}
    	void report_progress_to_mwnd(int steps)
    	{
    		mw_pb_->setMinimum(0);
    		mw_pb_->setMaximum(5);
    		mw_pb_->setValue(steps);
    	}
    };
    

    I thought that the report_progress_to_mwnd(int steps); function would be emited in the Thread of the MainWindow but it is still executed in the thread that I launched from the MainWindow. What do I oversee?

    Please do not recommend to change std::thread with QThread I need a solution with std::threads.

    Thanks for your help.

    Regards,
    Revi

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Hi, welcome to devnet.

      That's not how you define signals and slots and emitting a slot makes no sense.

      class Logger : public QObject
      {
         Q_OBJECT
      public:
         Logger(QProgressBar * mwpb) : mw_pb_(mwpb) {
             connect(this, SIGNAL(reported(int)), this, SLOT(report_progress_to_mwnd(int)), Qt::QueuedConnection);
         }
         void report(int step) {
            emit reported(step); //you emit a signal, not a slot
         }
      
      signals:
         void reported(int); //you don't implement signals. they are generated by moc
      
      public slots:
         void report_progress_to_mwnd(int steps) {
            mw_pb_->setMinimum(0);
            mw_pb_->setMaximum(5);
            mw_pb_->setValue(steps);
         }
      };
      

      Now calling a report method will emit signal reportedand that in turn will schedule the connected report_progress_to_mwnd slot to execute.

      Btw. Please use ``` at the beginning and end of code blocks to enable code highlighter.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Revi
        wrote on last edited by
        #3

        Thank you very much for the clarification that solved my problem.

        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