chap02->find
1.前置声明(不需要再提供定义中的所有细节)
#include <QDialog>
//前置声明
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
2.新类定义处加上 Q_OBJECT(是必须的)
class FindDialog: public QDialog
{
Q_OBJECT //必须的
3.典型的类定义
public:
FIndDialog(QWidget *parent=0) //构造函数
//典型的窗口部件类的定义方式,parent指定了他的父窗口部件,该参数的默认值是一个空指针,意味着没有父对象
4.singals关键字//定义事件触发时发射的信号
signals:
void findNext(const QString &str, Qt::CaseSensitivity cs);
void findPrevious(const QString &str, Qt::CaseSensitivity cs);
5.私有变量,我们使用类的前置声明,因为都是指针,而且没有必要在头文件中就去访问他们,因而编译程序就无需这些类的完整定义,就没有必要包含与这几个类相关的头文件
//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);
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
6
#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);
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
6.finddialog.cpp开始头文件包含区
#include<QtGui>
Qt是由数个模块组成的,像QtGui\QtOpenGL\QtSql等
7.tr()函数
caseCheckBox = new QCheckBox(tr("Match &case")); //tr函数把他翻译成其他> 语言的标记//在QObject对象以及包含Q_OBJECT宏的子类中都有这个函数的声明,在每> 一个用户可见的字符串周围使用tr()函数是以个不错的变成习惯
8.快捷键 &case快捷键c(即ALT+C)激活
9.行编辑器QLineEdit,并将他设置成label的伙伴(buddy),他可以在按下标签的快捷键时接受焦点
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
10.设置默认按钮,显示时即为热键
setDefault(true)//让Find按钮成为对话框的默认按钮
11.设置是否可用,setEnable(false)///不可用
12 connect链接
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()));
由于QObject是FindDialog的父对象之一,可以省略connect()前面的QObject::前缀,若不是这样需要加前缀
13.布局,QHBoxLayout\QVBoxLayout\QGridLayout不同的嵌套组合
14.类的关系
15sizeHint()函数设置一个理想的尺寸大小
//finddiag.cpp
#include <QtGui>
#include "finddialog.h"
FindDialog::FindDialog(QWidget *parent)
: QDialog(parent)
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case")); //tr函数把他翻译成其他语言的标记//在QObject对象以及包含Q_OBJECT宏的子类中都有这个函数的声明,在每一个用户可见的字符串周围使用tr()函数是以个不错的变成习惯
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);
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());
}
16.主程序
#include <QApplication>
#include "finddialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FindDialog *dialog = new FindDialog;
dialog->show();
return app.exec();
}
17运行
qmake -project
qmake find.pro //qmake 文件下的.pro文件
make
./find