ZeldaZach
ZeldaZach

Reputation: 538

Getting a QTimer in a QtConcurrent to work Properly

I am currently attempting to get a QTimer to work and emit the appropriate signals when running in a separate Thread. I'm not sure what I should do in order to get the timeout() signal to be emitted.

When I try using QFuture and QFutureWatcher they do not ever throw their finished() signal as this thread never really ends, it just keeps looping.

I've looked at a number of other questions 1, 2, 3, 4, 5 to no avail.

This is what I currently have. Any advice would be greatly appreciated!

Foo.cpp

Foo::Foo() {
    ...
    mcTimerForSpoilers = new QTimer(this);
    connect(mcTimerForSpoilers, SIGNAL(timeout()), this, SLOT(queueDownloadOfSpoilerFile()), Qt::QueuedConnection);
    QtConcurrent::run(this, &Foo::manageSpoilerTimer);
}

void Foo::manageSpoilerTimer() {
    ...
    mcTimerForSpoilers->setInterval(5000); // 5 seconds
    mcTimerForSpoilers->start();
}

void Foo::queueDownloadOfSpoilerFile() {
    ...
    std::cerr << "Queue Download" << std::endl;
    manageSpoilerTimer(); // Restart the timer
}

Upvotes: 0

Views: 843

Answers (1)

dtech
dtech

Reputation: 49289

Your design is wrong. You call QtConcurrent::run(this, &Foo::manageSpoilerTimer);but a timer can only be started from its own thread.

Furthermore, you don't specify an interval, so the timer will keep firing continuously, but then you also keep on starting the timer every time it times out.

It is not clear what you really want to do. It looks like you don't know what you are doing.

I don't think QtConcurrent is a good candidate for downloads to begin with. It doesn't support progress tracking, pausing or cancelling which are all features that downloading stuff should use. You should instead go for a threaded QObject based worker, as described in this answer.

Upvotes: 2

Related Questions