2

I'm trying to use QObject::connect to start a slot from after a thread finishes.

My class definition is:

class Test : public QWidget
{
public:
  Test(QWidget *parent=0);
private slots:
  void do_work();
  void show_box();
private:
  QFuture<void> work_thread;
  QFutureWatcher<void> watcher;
};

I tried the following code:

connect(&watcher, SIGNAL(finished()), this, SLOT(show_box()));

But when I run the compiled binary it says:

QObject::connect: No such slot QWidget::show_box()

I've also tried

QFutureWatcher<void> *watcher;
connect(watcher, &QFutureWatcher<void>::finished, this, &Test::show_box);

But it exits with a segmentation fault.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Agnibho Mondal
  • 421
  • 3
  • 13
  • You need to use the Q_OBJECT macro for signals and slots to work. I would expect moc to issue a warning about that. – drescherjm Dec 07 '15 at 00:26
  • 2
    Possible duplicate of [C++ Qt signal and slot not firing](http://stackoverflow.com/questions/3645898/c-qt-signal-and-slot-not-firing) – TheDarkKnight Dec 07 '15 at 09:49

1 Answers1

3

Missing Q_OBJECT inside Test.

What does the Q_OBJECT macro do? Why do all Qt objects need this macro?

If you don't have it, signals/slots don't work.

class Test : public QWidget{
  Q_OBJECT
public:
  Test(QWidget *parent=0);
private slots:
  void do_work();
  void show_box();
private:
  QFuture<void> work_thread;
  QFutureWatcher<void> watcher;
};
Community
  • 1
  • 1
Sarvadi
  • 550
  • 3
  • 13