简体   繁体   中英

Qt connect can't find slot

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.

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;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM