finddialog.h
/*******************************************************************************
防止对这个头文件的多重包含
*******************************************************************************/
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>//Qt中对话框的基类。QDialog从QWidget类中派生出来
/*******************************************************************************
以下4行前置声明了一些用于这个对话框实现中的Qt类。前置声明会告诉C++编译程序类的存在,而
不用提供类定义中的所有细节(通常放在它自己的头文件中)
*******************************************************************************/
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog : public QDialog
{
Q_OBJECT//对于所有定义了信号和槽的类,
//在类定义开始处的Q_OBJECT宏都是必须的
public:
FindDialog(QWidget *parent = 0);
/*FindDialog的构造函数就是一个典型的Qt窗口不见类的方式。parent参数指定了它的父窗口不见
。该参数的默认值是一个空指针,意味着对话框没有父对象*/
signals:
void findNext(const QString &str,Qt::CaseSensitivity cs);
void findPrevious(const QString &str,Qt::CaseSensitivity cs);
/****************************************************************************
singnals部分声明了当用户单击Find按钮时对话框所发射的两个信号。如果向前查询
(search backward)选项生效,对话框就发射findPrevious()信号,否则它就会发射
findNext()信号。
singnal关键字实际上是一个宏。C++预处理器会在编译程序找到它之前把它转换成标准
C++代码。Qt::CaseSensitivity是一个枚举类型,
它有Qt::CaseSensitive和Qt::CaseInsensitive两个值
*****************************************************************************/
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
include <QtGui>
#include "finddialog.h"
/*****************************************************************************
<QtGui>头文件包含了Qt GUI类的定义。Qt由数个模块组成,每个模块都有自己的类库。
最为重要的模块有QtCore、QtGui、QtNetwork、QtOpenGL、QtScript、QtSql,QtSvg
最为重要的模块有QtCore、QtGui、QtNetwork、QtOpenGL、QtScript、QtSql,QtSvg和QtXml。
若把<QtGui>包含在finddialog.h中,那么在头文件中再包含一个那么打的头文件着实不是一种好
的编程风格
******************************************************************************/
FindDialog::FindDialog(QWidget *parent)//构造函数
: QDialog(parent)//基类。把parent参数传递给了基类的构造函数
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));//tr()函数调用是把他们翻译成
//其它语言的标记
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton((tr("&Find")));
//findButton->setDefault(true);
findButton->setEnabled(false);
closeButton = new QPushButton(tr("Close"));
/*************************************************************************************
在这些字符串中,使用了表示“与”操作的符号“&”来表示快捷键。第20行创建了一个Find按钮
,用户可在那些支持快捷见的平台下通过按下Alt+F快捷见来激活它。第14行将lineEdit设置成了
label的“伙伴”(buddy),就是一个窗口不见,它可以在按下标签的快捷见识接收焦点(focus)。
所以当用户按下Alt+W时,焦点就会移动到这个行编辑器上
尽管也许并没有将你的应用程序立刻翻译成其它语言的打算,但在每一个用户可见的字符串周围
使用tr()函数还是一个很不错的习惯
*************************************************************************************/
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::前缀*/
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();//分隔符让find和close按钮占满空间
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
this->setWindowTitle(tr("Find"));
this->setFixedHeight(sizeHint().height());//QWidget::sizeHint()函数可以返回一
//个窗口部件所“理想”的尺寸大小
};
void FindDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs = caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;
if(backwardCheckBox->isChecked())
{
emit findPrevious(text,cs);//在别的程序中使用这个类时,可以通过关联这
//两个信号 知道这个窗口中lineEdit
//和caseCheckBox的状态
}
else
{
emit findNext(text,cs);
}
}
void FindDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}
main.cpp
#include <QApplication>
#include <finddialog.h>
int main(int argc,char **argv)
{
QApplication app(argc,argv);
FindDialog *dialog = new FindDialog;
dialog->show();
return app.exec();
/*******************************************************************************
防止对这个头文件的多重包含
*******************************************************************************/
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>//Qt中对话框的基类。QDialog从QWidget类中派生出来
/*******************************************************************************
以下4行前置声明了一些用于这个对话框实现中的Qt类。前置声明会告诉C++编译程序类的存在,而
不用提供类定义中的所有细节(通常放在它自己的头文件中)
*******************************************************************************/
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog : public QDialog
{
Q_OBJECT//对于所有定义了信号和槽的类,
//在类定义开始处的Q_OBJECT宏都是必须的
public:
FindDialog(QWidget *parent = 0);
/*FindDialog的构造函数就是一个典型的Qt窗口不见类的方式。parent参数指定了它的父窗口不见
。该参数的默认值是一个空指针,意味着对话框没有父对象*/
signals:
void findNext(const QString &str,Qt::CaseSensitivity cs);
void findPrevious(const QString &str,Qt::CaseSensitivity cs);
/****************************************************************************
singnals部分声明了当用户单击Find按钮时对话框所发射的两个信号。如果向前查询
(search backward)选项生效,对话框就发射findPrevious()信号,否则它就会发射
findNext()信号。
singnal关键字实际上是一个宏。C++预处理器会在编译程序找到它之前把它转换成标准
C++代码。Qt::CaseSensitivity是一个枚举类型,
它有Qt::CaseSensitive和Qt::CaseInsensitive两个值
*****************************************************************************/
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
include <QtGui>
#include "finddialog.h"
/*****************************************************************************
<QtGui>头文件包含了Qt GUI类的定义。Qt由数个模块组成,每个模块都有自己的类库。
最为重要的模块有QtCore、QtGui、QtNetwork、QtOpenGL、QtScript、QtSql,QtSvg
最为重要的模块有QtCore、QtGui、QtNetwork、QtOpenGL、QtScript、QtSql,QtSvg和QtXml。
若把<QtGui>包含在finddialog.h中,那么在头文件中再包含一个那么打的头文件着实不是一种好
的编程风格
******************************************************************************/
FindDialog::FindDialog(QWidget *parent)//构造函数
: QDialog(parent)//基类。把parent参数传递给了基类的构造函数
{
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
label->setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));//tr()函数调用是把他们翻译成
//其它语言的标记
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton((tr("&Find")));
//findButton->setDefault(true);
findButton->setEnabled(false);
closeButton = new QPushButton(tr("Close"));
/*************************************************************************************
在这些字符串中,使用了表示“与”操作的符号“&”来表示快捷键。第20行创建了一个Find按钮
,用户可在那些支持快捷见的平台下通过按下Alt+F快捷见来激活它。第14行将lineEdit设置成了
label的“伙伴”(buddy),就是一个窗口不见,它可以在按下标签的快捷见识接收焦点(focus)。
所以当用户按下Alt+W时,焦点就会移动到这个行编辑器上
尽管也许并没有将你的应用程序立刻翻译成其它语言的打算,但在每一个用户可见的字符串周围
使用tr()函数还是一个很不错的习惯
*************************************************************************************/
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::前缀*/
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();//分隔符让find和close按钮占满空间
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addLayout(leftLayout);
mainLayout->addLayout(rightLayout);
setLayout(mainLayout);
this->setWindowTitle(tr("Find"));
this->setFixedHeight(sizeHint().height());//QWidget::sizeHint()函数可以返回一
//个窗口部件所“理想”的尺寸大小
};
void FindDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs = caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;
if(backwardCheckBox->isChecked())
{
emit findPrevious(text,cs);//在别的程序中使用这个类时,可以通过关联这
//两个信号 知道这个窗口中lineEdit
//和caseCheckBox的状态
}
else
{
emit findNext(text,cs);
}
}
void FindDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}
main.cpp
#include <QApplication>
#include <finddialog.h>
int main(int argc,char **argv)
{
QApplication app(argc,argv);
FindDialog *dialog = new FindDialog;
dialog->show();
return app.exec();
}
from: http://blog.163.com/e_rommel/blog/static/1873830452012115192019/