用源码来实现文本查找
1 程序的运行结果如下:
2 程序的实现文件如下:
A:main.cpp
//the main file
#include <QApplication>
#include "finddialog.h"
int main(int argc,char * argv[])
{
QApplication app(argc,argv);
FindDialog * dialog =new FindDialog;
dialog->show();
return app.exec();
}
-----------------------------------------------------------------------------------
B:finddialog.cpp
//此源文件包括了FindDialog类的实现。
//头文件<QtGui>包括了QtCore和QtGui模块中所有类的实现。
//是在头文件中包含另外一个大的头文件是一个很坏的方式
#include <QtGui>
#include "finddialog.h"
//define one function
FindDialog::FindDialog(QWidget * parent)
:QDialog(parent)
{
//函数tr(),这些字符串被标记为可以翻译成别的语言。
//it is a good plan to use the tr(),
/*
用操作符’&’来指出快捷键。在第20行我们创建了一个拥有快捷键(Alt+W)的标签
,在第23行我们给这个标签设置一个伙伴(buddy):lineEdit。
一个buddy就是当标签的快捷键被按下的时候,接收程序焦点的窗口。
所以,当用户按下Alt+W的时候,程序焦点转移到字符编辑框上。
*/
label = new QLabel(tr("Find &what:"));
lineEdit = new QLineEdit;
//buddy是“同伴,伙伴”的意思
label -> setBuddy(lineEdit);
caseCheckBox = new QCheckBox(tr("Match &case"));
backwardCheckBox = new QCheckBox(tr("Search &backward"));
findButton = new QPushButton(tr("&Find"));
/*
通过调用函数setDefault(true),我们将按钮Find设置为程序的默认按钮
(所谓的默认按钮就是当用户按下回车键时被触发的按钮)。
在第34行,我们将按钮Find设置为不可用。当一个窗口被设置为不可用的时候,
它通常显示为灰色,并不会和用户产生任何交互。
*/
findButton ->setDefault(true);
findButton ->setEnabled(false);
closeButton = new QPushButton(tr("Close"));
//由于QObject是FindDialog的一个父类,
//所以我们可以在调用connect()函数时忽略前面的前缀QObject:: 。
connect(lineEdit,SIGNAL(textChanged(const QString &)),
this,SLOT(enableFindButton(const QString &)));
connect(findButton,SIGNAL(clicked()),
this,SLOT(findClicked()));
//槽close()是从QWidget继承而来的,它的默认行为是隐藏窗口对象(而不是删除它)。
connect(closeButton,SIGNAL(clicked()),
this,SLOT(close()));
//below is layout
//布局管理器可以包括窗口,也可以包括其它的布局管理器。
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);//最外层的布局是主要的布局,它在这一行被安装并负责响应对话框的全部区域。
//up code is dealing with the father and son,put the son int
//the father.
//在一个正在运行的程序当中,布局是不可见的。
setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());
}
//父窗口被销毁的时候,Qt将会自动删除所有的子对象。
void FindDialog::findClicked()
{
QString text = lineEdit->text();
Qt::CaseSensitivity cs =
caseCheckBox->isChecked()?Qt::CaseSensitive
:Qt::CaseInsensitive;
if(backwardCheckBox->isChecked())
{
emit findPrevious(text,cs);
}
//关键字emit在Qt里很特殊,和其它的Qt扩展名一样,
//它在被传递给标准C++编译器之前会被C++预处理器转换。
else
{
emit findNext(text,cs);
}
}
void FindDialog::enableFindButton(const QString &text)
{
findButton->setEnabled(!text.isEmpty());
}
//up code is about the slots.
----------------------------------------------------------------------------------
C: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也是一个宏,
//Qt::CaseSecsitivity是一个枚举类型。它可以代表值Qt:: CaseSensitive和Qt::CaseInsensitive。
signals:
void findNext(const QString & str,Qt::CaseSensitivity cs);
void findPrevious(const QString & str, Qt::CaseSensitivity cs);
//关键字slots也是一个构造后可以被C++编译器辩识的宏。
private slots:
void findClicked();
void enableFindButton(const QString & text);
private:
QLabel * label;
QLineEdit * lineEdit;
QCheckBox * caseCheckBox;
QCheckBox * backwardCheckBox;
QPushButton * findButton;
QPushButton * closeButton;
};
#endif
--------------------------------------------------------------------FINISHED.