现在来看类的源文件
finddialog.cpp
#include<QtGui> //包括了很多,但是很大,不是很好的编程风格
#include"finddialog.h"
FindDialog::FindDialog(QWidget *parent)
:QDialog(parent) //构造函数 C++知识 把parent 参数传递给基类的构造函数
{
label=new QLabel(tr("Find &what")); //tr()函数的处理过的字符串可以使用工具提取出来翻译成其他语言
lineEdit =new QLineEdit; //行编辑器部件
label->setBuddy(lineEdit); // 设置伙伴 字符串中的&代表快捷键 用快捷键激活时,焦点传递给lineEdit
caseCheackBox=new QCheckBox(tr("Match &case"));
backwardCheckBox=new QCheckBox(tr("Search &backward"));
findButton=new QPushButton(tr("&Find"));
findButton->setDefault(true); //该函数让find按钮成为默认按钮 ,默认就是enter键下的按钮
findButton->setEnabled(false);//禁用find按钮 灰色
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()));//close 从QWideget继承过来,默认从用户视野隐藏
//下面的布局管理器 可以从下网上看,先给我们的主类加上一个mainLayout,在mainLayout的左边加上一个leftLayout,
//右边加上一个rightLayout, 一次在分别网上加,知道所有私有对象全部得到布局
QHBoxLayout *topLeftLayout=new QHBoxLayout;
topLeftLayout->addWidget(label);
topLeftLayout->addWidget(lineEdit);
QVBoxLayout *leftLayout=new QVBoxLayout;
leftLayout->addLayout(topLeftLayout);
leftLayout->addWidget(caseCheackBox);
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);//安装在FindDialog中
setWindowTitle(tr("Find"));
setFixedHeight(sizeHint().height());//返回一个理想的尺寸大小
}
//对话框中所用到的槽
void FindDialog::findClicked()
{QString text =lineEdit->text();
Qt::CaseSensitivity cs=caseCheackBox->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());
}
在这一节对findClicked()槽的解释不是很多,这会在第三章得到应用,