插槽未在QT应用程序中接收信号

时间:2012-06-15 09:56:14

标签: c++ multithreading qt signals-slots

我有一个奇怪的情况,我有一个工作线程做它的事情,然后发出信号回调GUI线程关闭一个对话框。有人可以解释为什么这有效:

的WorkerThread:

[Header]
signals:
    void writeComplete(void);

[Source]
void startWorkerThread()
{
    // do some stuff in boost::thread
    emit writeComplete();
}

MainWindow子类:

burnDialog = new QProgressDialog("Writing to disc", "", 0, 0);
burnDialog ->setCancelButton(0);
QWidget::connect( discHandler.get(), SIGNAL(writeComplete()), burnDialog, SLOT(close()) );
QWidget::connect( discHandler.get(), SIGNAL(writeComplete()), this, SLOT(close()) );
burnDialog->open();
discHandler->startWorkerThread();

但这不是:

MainWindow子类:     [页眉]     公共位置:         void closeWithDialog(void);

[Source]
burnDialog = new QProgressDialog("Writing to disc", "", 0, 0);
QWidget::connect( discHandler.get(), SIGNAL(writeComplete()), this, SLOT(closeWithDialog()) );
burnDialog ->setCancelButton(0);
burnDialog->open();
discHandler->startWorkerThread();

void closeWithDialog()
{
    burnDialog->close();
    close();
}

2 个答案:

答案 0 :(得分:3)

感觉像......我没有把Q_OBJECT放在头文件中。我假设子类继承QMainWindow Q_OBJECT接口也将继承隐含的事实。但它不是......感谢你们的帮助!

答案 1 :(得分:0)

以下是无效案例中的错误行:

QWidget::connect( discHandler.get(), SIGNAL(writeComplete()), burnDialog,
                  SLOT(closeWithDialog()) );

您要为SLOT实例分配burnDialog,这意味着closeWithDialog()方法必须为QProgressDialog类。 QProgressDialog没有这样的方法。您应该检查控制台是否有以下消息:

Object::connect: No such slot QProgressDialog::closeWithDialog()

将以上错误的行更改为以下内容:

QWidget::connect( discHandler.get(), SIGNAL(writeComplete()), this,
                  SLOT(closeWithDialog()) );