在网上阅读了QT入门教程系列文章,感谢豆豆博客的版主,把这么好的教程和大家分享,本文是对入门教程的笔记,以期抛砖引玉,听到大家的好见解。 希望大家更好更快的学习QT,达到自己的目标,实现自己的理想。
本文分析QT项目的结构,如头文件中代码的结构与功效,主源代码文件的结构与功效。也就是说头文件中应该放些什么,源代码文件中放些什么。
先看一个经典的例子,头文件:
- #ifndef FINDDIALOG_H
- #define FINDDIALOG_H
- #include <QtGui/QDialog>
- class QCheckBox;
- class QLabel;
- class QLineEdit;
- class QPushButton;
- class FindDialog : public QDialog
- {
- Q_OBJECT
- public:
- FindDialog(QWidget *parent &#61; 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_H
主函数&#xff1a;
- #include <QApplication>
- #include "finddialog.h"
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- FindDialog *dialog &#61; new FindDialog;
- dialog->show();
- return app.exec();
- }
主源代码函数&#xff1a;
- #include <QtGui>
- #include "finddialog.h"
- FindDialog::FindDialog(QWidget *parent)
- : QDialog(parent)
- {
- label &#61; new QLabel(tr("Find &what:"));
- lineEdit &#61; new QLineEdit;
- label->setBuddy(lineEdit);
- caseCheckBox &#61; new QCheckBox(tr("Match &case"));
- backwardCheckBox &#61; new QCheckBox(tr("Search &backford"));
- findButton &#61; new QPushButton(tr("&Find"));
- findButton->setDefault(true);
- findButton->setEnabled(false);
- closeButton &#61; 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 &#61; new QHBoxLayout;
- topLeftLayout->addWidget(label);
- topLeftLayout->addWidget(lineEdit);
- QVBoxLayout *leftLayout &#61; new QVBoxLayout;
- leftLayout->addLayout(topLeftLayout);
- leftLayout->addWidget(caseCheckBox);
- leftLayout->addWidget(backwardCheckBox);
- QVBoxLayout *rightLayout &#61; new QVBoxLayout;
- rightLayout->addWidget(findButton);
- rightLayout->addWidget(closeButton);
- rightLayout->addStretch();
- QHBoxLayout *mainLayout &#61; new QHBoxLayout;
- mainLayout->addLayout(leftLayout);
- mainLayout->addLayout(rightLayout);
- setLayout(mainLayout);
- setWindowTitle(tr("Find"));
- setFixedHeight(sizeHint().height());
- }
- FindDialog::~FindDialog()
- {
- }
- void FindDialog::findClicked()
- {
- QString text &#61; lineEdit->text();
- Qt::CaseSensitivity cs &#61; caseCheckBox->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive;
- if(backwardCheckBox->isChecked()) {
- emit findPrevious(text, cs);
- } else {
- emit findNext(text, cs);
- }
- }
- void FindDialog::enableFindButton(const QString &text)
- {
- findButton->setEnabled(!text.isEmpty());
- }
程序的运行结果是这样的&#xff1a;