qt之旅-1纯手写Qt界面

来源:互联网 发布:lol 出装 数据网站 编辑:程序博客网 时间:2024/06/14 03:52
通过手写qt代码来认识qt程序的构成,以及特性。设计一个查找对话框。下面是设计过程
1 新建一个empty qt project
2 配置pro文件
HEADERS += \    Find.hQT += widgetsSOURCES += \    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_OBJECTpublic:    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,程序会进入事件循环状态。}





0 0
原创粉丝点击