C++ 未调用Qt插槽函数
我无法理解为什么在连接函数后没有正确调用它 .h文件C++ 未调用Qt插槽函数,c++,qt,user-interface,C++,Qt,User Interface,我无法理解为什么在连接函数后没有正确调用它 .h文件 class NewCustomer : public QObject { Q_OBJECT public: QWidget* newCustomer = new QWidget; QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); NewCustome
class NewCustomer : public QObject {
Q_OBJECT
public:
QWidget* newCustomer = new QWidget;
QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
NewCustomer(QObject *parent);
~NewCustomer();
private slots:
void aAccepted();
};
.cpp文件
void thisfunctionworksthough() {
QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.exec();
}
NewCustomer::NewCustomer(QObject *parent) : QObject(parent) {
connect(this->buttonBox, &QDialogButtonBox::accepted, this, &NewCustomer::aAccepted);
connect(buttonBox, &QDialogButtonBox::rejected, thisfunctionworksthough);
newCustomer->setMinimumSize(700, 600);
newCustomer->show();
}
void NewCustomer::aAccepted() {
QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.exec();
}
free函数可以工作,但类中定义为slot的函数不能工作。为什么?我真是太蠢了,但显然我是按值创建类实例的,所以在作用域结束时它被删除了。我确保它不会这么快从内存中释放出来,现在它就像一个符咒一样工作。一个映射到Ok按钮(已接受),另一个映射到Cancel按钮(已拒绝)。当我按下Ok时,什么都没有发生,当我按下Cancel时,我得到了消息。这段代码编译吗?@H.G是的,尽管我删除了includes和一些其他小部件以提供一个简单的示例。