【问题标题】:Qt connect can't find slotQt连接找不到插槽
【发布时间】:2016-03-11 12:28:12
【问题描述】:

我正在尝试使用QObject::connect 在线程完成后启动一个槽。

我的班级定义是:

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

我尝试了以下代码:

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

但是当我运行编译后的二进制文件时,它会说:

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

我也试过

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

但它会因分段错误而退出。

【问题讨论】:

标签: c++ qt


【解决方案1】:

Test 中缺少Q_OBJECT

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

如果你没有它,信号/插槽就不起作用。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 2016-11-10
    相关资源
    最近更新 更多