2021-08-05 Qt会在删除父对象的时候自动删除其所属的所有子对象
作者:互联网
#pragma once
#include <QDialog>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog :
public QDialog
{
Q_OBJECT
public:
FindDialog(QWidget* parent = 0);
virtual ~FindDialog();
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;
};
FindDialog::FindDialog(QWidget* parent) :QDialog(parent)
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton(tr("&Find"));
findButton->setDefault(true);
findButton->setEnabled(false);
closeButton = new QPushButton(tr("Close"));
connect(lineEdit, SIGNAL(textChanged(const QString&)),
this, SLOT(enableFindButton(const QString&)));
connect(findButton, SIGNAL(clicked()),
this, SLOT(findClicked()));
connect(closeButton, SIGNAL(clicked()),
this, SLOT(close()));
QHBoxLayout* topLeftLayout = new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);
QVBoxLayout* leftLayout = new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheckBox);
leftLayout->addWidget(backwardCheckBox);
QVBoxLayout* rightLayout = new QVBoxLayout;
rightLayout->addWidget(findButton);
rightLayout->addWidget(closeButton);
rightLayout->addStretch();
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
setWindowTitle("Find");
setFixedHeight(sizeHint().height());
}
FindDialog::~FindDialog()
{
}
由于在创建这个对话框中的窗口部件和布局时使用的是new,所以需要写一个能够调用delete的析构函数,以便可以删除所创建的每一个窗口部件和布局。但是这样做并不是必需的,因为Qt会在删除父对象的时候自动删除其所属的所有子对象,也就会删除FindDialog中作为其子孙的所有子窗口部件和子布局。
标签:findButton,const,Qt,删除,FindDialog,05,tr,addWidget,new 来源: https://blog.csdn.net/weixin_44819948/article/details/119423963