我想將按鈕連接到client::prework()
。經過簡單的調試後,我發現client::prework()
沒有被調用(沒有任何部分正在執行),但QObject::connect
調用返回true
。儘管連接成功,插槽仍未調用
client.h
class client : public QObject
{
//some declarations
public slots:
int prework();
};
client.cpp
void client::prework()
{
//implementation
}
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QWidget * wdg = new QWidget(this);
QVBoxLayout *vlay = new QVBoxLayout(wdg);
QPushButton *btn1 = new QPushButton("connectme");
vlay->addWidget(btn1);
client obj1;
qDebug()<<"h";
QObject::connect(btn1,SIGNAL(clicked()),&obj1,SLOT(prework()));
// obj1.prework();
}
您忘記了將'Q_OBJECT'宏添加到'client'聲明中。請參閱:http://stackoverflow.com/questions/1368584/qt-question-what-does-the-q-object-macro-do-why-do-all-qt-objects-need-this-ma – Nejat 2015-02-09 06:42:29
您聲明'prework'在標題中返回'int',但是在你的.cpp中,你將它定義爲'void'。這是一個錯字,對嗎? – thuga 2015-02-09 08:01:49