(QT) Чистый виртуальный метод с именем terminate, вызываемый без активного исключения - PullRequest
0 голосов
/ 05 августа 2020

Я новичок в C ++ и QT и получаю эту странную ошибку:

pure virtual method called terminate called without an active exception

У меня всего три простых файла: DialogBox.cpp, DialogBox.h и Main.cpp.

ГЛАВНЫЙ. 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 Ответ

2 голосов
/ 05 августа 2020

Qt полагается на необработанные указатели. Вы должны создавать виджеты с новыми. Хорошей новостью является то, что он обеспечивает своего рода управление памятью в отношениях между родителями и детьми. Каждый раз, когда вы создаете виджет, вы предоставляете ему родителя. Когда родительский элемент уничтожен, он уничтожает всех дочерних элементов.

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");
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...