在这本书的第二章,给了一个例子
头文件finddialog.h
- C/C++ code
#ifndef FINDDIALOG_H#define FINDDIALOG_H#include <QDialog>class QCheckBox;class QLabel;class QLineEdit;class QPushButton;class FindDialog : public QDialog{ Q_OBJECTpublic: FindDialog(QWidget *parent = 0);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的部分代码
- C/C++ code
#include <QtGui>#include "finddialog.h"FindDialog::FindDialog(QWidget *parent)//疑问一: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);//疑问2:为什么不是“对象.setLayout(mainLayout);”的形式 setWindowTitle(tr("Find"));//疑问同上 setFixedHeight(sizeHint().height());//疑问同上}
------解决方案--------------------
1.初学不必深究parent指针,用Qt-creator创建项目会自动写入。如果想闹明白:http://devbean.blog.51cto.com/448512/214166
2.this->setLayout(mainLayout);
this省略。