Qt Slot函数未被调用

时间:2016-10-10 22:19:09

标签: c++ qt user-interface

我不能为我的生活找出为什么我的功能在连接后没有被正确调用。

.h文件

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

自由函数可以正常工作,但是类中定义为slot的那个函数并不适用。为什么呢?

1 个答案:

答案 0 :(得分:0)

这对我来说非常愚蠢,但显然我是按值创建了类实例,所以当范围结束时它会被删除。我确保它不会如此迅速地从记忆中解脱出来,现在它就像一个魅力。