我这个例子是《C++ GUI Programming with Qt 4》第一版里面的例子,我自己增加的析构函数。请看代码:
findDialog.h:
- #ifndef _FINDDIALOG_H_
- #define _FINDDIALOG_H_
- #include <QDialog>
- class QCheckBox;
- class QLabel;
- class QLineEdit;
- class QPushButton;
- class FindDialog : public QDialog
- {
- Q_OBJECT
- public:
- FindDialog(QWidget *parent = 0);
- ~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;
- };
- #endif
而后是findDialog.cpp:
- #include <QtGui>
- #include "findDialog.h"
- FindDialog::~FindDialog()
- {
- delete label;
- if(label != NULL)
- {
- label = NULL;
- }
-
- delete lineEdit;
- if(lineEdit != NULL)
- {
- lineEdit = NULL;
- }
-
- delete caseCheckBox;
- if(caseCheckBox != NULL)
- {
- caseCheckBox = NULL;
- }
- delete backwardCheckBox;
- if(backwardCheckBox != NULL)
- {
- backwardCheckBox = NULL;
- }
- delete findButton;
- if(findButton != NULL)
- {
- findButton = NULL;
- }
- }
- 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);
-
- /********************
- delete topLeftLayout;
- if(topLeftLayout != NULL)
- {
- topLeftLayout = NULL;
- }
- **********/
-
- setWindowTitle(tr("Find"));
- setFixedHeight(sizeHint().height());
- }
- void FindDialog::findClicked()
- {
- QString text = lineEdit->text();
- Qt::CaseSensitivity cs =
- caseCheckBox->isChecked() ? Qt::CaseSensitive
- : Qt::CaseInsensitive;
- if (backwardCheckBox->isChecked()) {
- emit findPrevious(text, cs);
- } else {
- emit findNext(text, cs);
- }
- }
- void FindDialog::enableFindButton(const QString &text)
- {
- findButton->setEnabled(!text.isEmpty());
- }
最后main.cpp:
- #include <QApplication>
- #include "finddialog.h"
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- FindDialog *dialog = new FindDialog;
- dialog->show();
- //delete dialog;
- return app.exec();
- }
整个pro编译是没有问题的。
疑问在下面两个地方,依照我们学习C++的思想,每当new出来一个对象后,使用该对象完毕需要delete掉。为啥在他们提供的例子里面:
第一个,他们在构造函数里面new出来的对象,并没有在析构函数里面delete(该类的析构函数内容是我填写的)。
第二个,在构造函数里面new出来的那几个QHBoxLayout对象,在结束的时候不能够delete掉,delete掉后,运行起来该布局就不见了。
第三个,在main.cpp里面,new出来的dialog对象,当show()完后,为啥不能够delete掉?因为当delete掉后,整个窗体都不出来了。
P.S.:我是在winxp上
安装的QT4.4,然后使用QT命令行运行的该程序。
谢谢!