通过手写qt代码来认识qt程序的构成,以及特性。设计一个查找对话框。以下是设计过程

1 新建一个empty qt project

2 配置pro文件

HEADERS += \
Find.h
QT += widgets SOURCES += \
Find.cpp \
main.cpp

3 编写对话框的类

代码例如以下:

//Find.h
#ifndef FIND_H
#define FIND_H
#include <QDialog>
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog : public QDialog
{
Q_OBJECT
public:
FindDialog(QWidget *parent = NULL);
signals:
void findNext(const QString &str,Qt::CaseSensitivity cs);
void findPrevious(const QString &str,Qt::CaseSensitivity cs);
private slots:
void findClicled();
void enableFindButton(const QString &text);
private:
QLabel* label;
QLineEdit* lineEdit;
QCheckBox* caseCheckBox;
QCheckBox* backwardCheckBox;
QPushButton* findButton;
QPushButton* closeButton;
};
#endif // FIND_H
//Find.cpp
#include <QtGui>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QCheckBox>
#include "Find.h" FindDialog::FindDialog(QWidget *parent)
{
label = new QLabel(tr("Find &what"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit); caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Serach &backward")); findButton = new QPushButton(tr("&Find"));
closeButton = new QPushButton(tr("&Close"));
findButton->setDefault(true);
findButton->setEnabled(false); 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(tr("Find"));
setFixedHeight(sizeHint().height());
} void FindDialog::findClicled()
{
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)
{
}
//main.cpp
#include <QApplication>
#include "Find.h" int main(int argc,char* argv[])
{
QApplication app(argc,argv);
FindDialog *dialog = new FindDialog;
dialog->show();
return app.exec();
}

3 观察程序 类定义中的Q_OBEJECT

Q_OBEJECT是一个宏,对全部定义了信号和槽的类。在类定义開始处的Q_OBJECT宏是必须的。而且要直接或者间接的继承QObject





4 signals

<1>signals也是一个宏,能够在Qt的文件里看到Signals 就是 public,所以其前面不能加不论什么限定符.

<2>signals会被moc自己主动生成,所以一定不要在cpp文件里实现signals的函数

<3>signals的返回值仅仅能是void。

<4>signals中的函数原型必须和slots中的原型一致。例外的当slots中的參数比signals中少的时候。signal中的后面多余的參数会被忽略。

<5>signals 能够有默认參数



5 slots

<1>slots 是普通的C++函数,前面能够加限定符,public。private,protected,virtual。

<2>slots能够有默认參数

<3> signals 和slots的机制非常像回调函数机制,就是用函数指针指向函数。



6 connect函数--信号和槽的连接

connec(sender,SIGNALE(xx),receiver,SLOTS(yy));

一个信号能够连接多个槽;多个信号能够连接到一个槽;一个信号能够和信号连接;连接能够被移除。



7 qt会在删除父对象的时候会自己主动删除全部的子对象,所以来写析构函数来释放新建的部件和布局是多余的。

8 布局

<1>Layout能够加入widget;

<2>Layout能够加入Layout

<3>widget能够加入Layout

<4>QVBoxLayout。QHBoxLayout。分别表示纵向,横向布局。

<5>程序终于一定要有个Layout来罩住全部的Layout。





9 main函数解析

#include <QApplication>
#include "Find.h"
int main(int argc,char* argv[])
{
QApplication app(argc,argv); //app用来管理整个应用程序使用到的资源
FindDialog *dialog = new FindDialog;
dialog->show();
return app.exec(); //将应用程序的控制权交给qt,程序会进入事件循环状态。
}
05-11 21:50