problems with Qt::QueuedConnection



  • 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


  • Moderators

    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.



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


Log in to reply
 

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