(QT)在没有活动异常的情况下调用了称为终止的纯虚拟方法

时间:2020-08-05 09:01:46

标签: c++ qt

我是C ++和QT的新手,并且遇到了这个奇怪的错误:

pure virtual method called terminate called without an active exception

我只有三个简单的文件:DialogBox.cppDialogBox.hMain.cpp

MAIN.cpp

#include "DialogBox.h"
#include "qapplication.h"
#include "qboxlayout.h"
#include "qnamespace.h"
#include "qwidget.h"
#include "qslider.h"
#include "qspinBox.h"
#include "QHBoxLayout"

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    DialogBox dialogBox{};
    dialogBox.show();

    return app.exec();
}

DialogBox.h

#ifndef DIALOG_BOX_H_
#define DIALOG_BOX_H_

#include "QDialog"
#include "QLabel"
#include "QLineEdit"
#include "QCheckBox"
#include "QPushButton"

class DialogBox : public QDialog {

    Q_OBJECT

public:
    DialogBox();
    DialogBox(QWidget* parent);
    ~DialogBox();

signals:
    void findNext(const QString& str, Qt::CaseSensitivity cs);
    void findPrevious(const QString& str, Qt::CaseSensitivity cs);

private slots:
    void findClicked();
    void enableFindButton(const QString& text);

private:
    QLabel label;
    QLineEdit lineEdit;
    QCheckBox caseCheckBox;
    QCheckBox backwardCheckbox;
    QPushButton findButton;
    QPushButton closeButton;
};

DialogBox.cpp

#include "DialogBox.h"
#include "qboxlayout.h"

DialogBox::DialogBox()
    : DialogBox{nullptr} {
}

DialogBox::DialogBox(QWidget* parent)
    : label{}, lineEdit{}, caseCheckBox{}, backwardCheckbox{}, findButton{}, closeButton{}, QDialog{parent} {

    label.setText("Find &what:");
    caseCheckBox.setText("Match &case");
    backwardCheckbox.setText("Search &backward");
    findButton.setText("&Find");
    findButton.setDefault(true);
    findButton.setEnabled(false);
    closeButton.setText("Close");

    QHBoxLayout topLeftLayout{};
    topLeftLayout.addWidget(&label);
    topLeftLayout.addWidget(&lineEdit);

    QVBoxLayout leftLayout{};
    leftLayout.addLayout(&topLeftLayout);
    leftLayout.addWidget(&caseCheckBox);
    leftLayout.addWidget(&backwardCheckbox);

    QVBoxLayout rightLayout{};
    rightLayout.addWidget(&findButton);
    rightLayout.addWidget(&closeButton);
    rightLayout.addStretch();

    QHBoxLayout mainLayout{};
    mainLayout.addLayout(&leftLayout);
    mainLayout.addLayout(&rightLayout);
    setLayout(&mainLayout);

    setWindowTitle("Find");
    //setFixedHeight(sizeHint().height());
}

DialogBox::~DialogBox() {


}

void DialogBox::findClicked() {


}

void DialogBox::enableFindButton(const QString& text) {


}

1 个答案:

答案 0 :(得分:2)

Qt依赖原始指针。您必须使用new创建小部件。好消息是它提供了一种具有父子关系的内存管理。每次创建窗口小部件时,都为其提供父项。当父母被摧毁时,它会摧毁所有的孩子。

main.cpp

#include <QApplication>
#include "dialogbox.h"

int main(int argc, char* argv[])
{
    QApplication app(argc, argv);
    // create dialog here, all its children will be created and parented in constructor
    // no parent here, that is why we need a default value in constructor
    DialogBox dialogBox;
    dialogBox.show();

    // when dialog is destroyed it will destroy all of its children
    return app.exec();
}

dialogbox.h

#ifndef DIALOG_BOX_H_
#define DIALOG_BOX_H_

#include "QDialog"
#include "QLabel"
#include "QLineEdit"
#include "QCheckBox"
#include "QPushButton"

class DialogBox : public QDialog {
    Q_OBJECT
public:
    DialogBox(QWidget* parent = nullptr); // one constructor with default value for parent

signals:
    void findNext(const QString& str, Qt::CaseSensitivity cs);
    void findPrevious(const QString& str, Qt::CaseSensitivity cs);

private slots:
    void findClicked() {};
    void enableFindButton(const QString& text) {};

private:
    // raw pointers here
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *caseCheckBox;
    QCheckBox *backwardCheckbox;
    QPushButton *findButton;
    QPushButton *closeButton;
};

#endif

dialogbox.cpp

#include "dialogbox.h"
#include "qboxlayout.h"

DialogBox::DialogBox(QWidget* parent)
    : QDialog{parent}, // pay attention to the order of initialization
      label{new QLabel(this)},
      lineEdit{new QLineEdit(this)},
      caseCheckBox{new QCheckBox(this)},
      backwardCheckbox{new QCheckBox(this)},
      findButton{new QPushButton(this)},
      closeButton{new QPushButton(this)}
{
    label->setText("Find &what:");
    caseCheckBox->setText("Match &case");
    backwardCheckbox->setText("Search &backward");
    findButton->setText("&Find");
    findButton->setDefault(true);
    findButton->setEnabled(false);
    closeButton->setText("Close");

    // no parent - deliberately
    // because parent is used not only for memory management, but to tell the widgets about their relationship. 
    // Here this layout is not a child of dialog, it will become the child of another layout later.
    auto topLeftLayout = new QHBoxLayout(); 
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);

    auto leftLayout = new QVBoxLayout();
    leftLayout->addLayout(topLeftLayout);   // leftLayout will parent topLeftLayout itself
    leftLayout->addWidget(caseCheckBox);
    leftLayout->addWidget(backwardCheckbox);

    auto rightLayout = new QVBoxLayout();
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
    rightLayout->addStretch();

    auto mainLayout = new QHBoxLayout();
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
    setLayout(mainLayout);

    setWindowTitle("Find");
}