[Interest] QFutureWatcher not emitting signals

Bob Hood bhood2 at comcast.net
Thu Nov 24 04:46:29 CET 2016


Nevermind.  It's a problem at the macro level.  This will only work when the 
Tasklet class is created from the main thread.  I was creating it from deep 
down inside calls originating from a QThread object, which is apparently a no-no.

I'll refactor to notify the main thread to create these instances, and all 
should be well.

Sorry to bother.


On 11/23/2016 8:16 PM, Bob Hood wrote:
> I did some Googling about this, and found a LOT of hits about QFutureWatcher 
> not emitting its signals (like finished()) when the QFuture it's watching 
> completes.  However, in all cases I've seen, people were using an auto 
> version of QFutureWatcher which dropped out of scope on them (i.e., 
> allocated on the local stack). Allocating it on the heap seemed to solve all 
> problems I found.
>
> Well, it isn't solving it for me.  I'm using Qt 5.6.2.  I have a simple 
> tasklet class that performs a discrete function in a separate thread 
> independent of the main process.  Its basic structure looks like this:
>
> Definition:
>
>     class Tasklet : public QObject
>     {
>         Q_OBJECT
>     public:
> Tasklet(...args...);
>         ~Tasklet();
>
>     signals:
>         void    signal_result(const QString& reference, bool result);
>
>     private slots:
>         void    slot_future_finished();
>
>     private:    // methods
>         bool    run();
>
>     private:    // data members
>         ...args...
>
>         QFutureWatcher<bool>*   watcher;
>     };
>
> Implementation:
>
>     Tasklet::Tasklet(...args...) : ...init...
>     {
>         QFuture<bool> future = QtConcurrent::run(this, &Tasklet::run);
>         watcher = new QFutureWatcher<bool>();
>         connect(watcher, &QFutureWatcher<bool>::finished, this, 
> &Tasklet::slot_future_finished);
>         watcher->setFuture(future);
>     }
>
>     bool Tasklet::run()
>     {
>         ...processing...
>         return true;
>     }
>
>     void Tasklet::slot_future_finished()
>     {
>         emit signal_result(reference, watcher->result());
>         watcher->deleteLater();
>     }
>
> Tasklet::slot_future_finished() is NEVER called. I've verified that the 
> connect() returns true, tried using the old-style syntax for connect, and 
> I've also tried having a class member for QFutureWatcher instead of 
> allocating it on the heap. No joy.
>
> Can another pair of eyes see what am I doing wrong here?
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>




More information about the Interest mailing list