0

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
}
ZeldaZach
  • 478
  • 1
  • 9
  • 18
  • Generally speaking, timers don't need their own threads. Whatever made you think they do is incorrect and you should rethink the design. Perhaps edit the question to tell us what you're trying to do, not how you're doing it (because it's broken). – Kuba hasn't forgotten Monica Jul 03 '17 at 17:54

1 Answers1

2

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.

dtech
  • 47,916
  • 17
  • 112
  • 190
  • Wasn't 100% sure what I was doing, but I figured it out by making a new Thread with the timer in it. Thanks for the help – ZeldaZach Jul 03 '17 at 06:25