Qt4学习-SPREADSHEET之FindDialog

本文介绍在Qt4中如何利用FindDialog类进行查找操作,通过`finddialog.h`和`finddialog.cpp`文件的代码实现,详细探讨了信号与槽的连接以及对象的交互。

 

"finddialog.h" code:

/*避免多重包含*/
#ifndef FINDDIALOG_H
#define FINDDIALOG_H

/*QDialog继承了QWidget*/
#include <QDialog>

/*前置声明,告诉编译器类的存在,使编译更快些*/
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

/*定义FindDialog类作为QDialog的子类*/
class FindDialog : public QDialog
{

/*拥有信号和槽的类必须有Q_OBJECT宏*/
 Q_OBJECT
public:

/*parent参数指定了父窗口部件,默认情况下是个空指针,意味着它没有父窗口部件*/
 FindDialog(QWidget *parent = 0); 

/**声明公共信号
*singals是个宏,预处理器会把它转换成标准c++
*/
signals:

/**声明两个信号,这里&str是引用
*当用户点击find按钮,如果前向查询选中,发射findPrevious信号
*否则发射findNext信号。
*
*Qt::CaseSensitivity是个枚举类型,值分别可以为Qt::CaseSensitivity或Qt::CaseInsensitivity。
*/
 void findNext(const QString &str,Qt::CaseSensitivity cs);
 void findPrevious(const QString &str,Qt::CaseSensitivity cs);

/*声明私有槽,与signals一样是个宏*/
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" code:

/*QtGui中包含着QtCore和QtGui模块的所有类的定义,包含这个头文件就不用逐个包含所有类了*/
#include <QtGui>
#include "finddialog.h"

/*将parent参数传递给父类的构造函数*/
FindDialog::FindDialog(QWidget *parent) : QDialog(parent)
{

/*创建控件,tr()可使字符自动转换成其他语言,有tr()是个好习惯
*&表示快捷键,即"Alt + w"
*/
 label = new QLabel(tr("Find &what:"));
 lineEdit = new QLineEdit;

/*设置友好控件,即当用户按下"Alt + w"时,焦点会转移到该标签的友好控件*/
 label->setBuddy(lineEdit);
 caseCheckBox = new QCheckBox(tr("Match &case"));
 backwardCheckBox = new QCheckBox(tr("Search &backward"));
 findButton = new QPushButton(tr("&Find"));
 
/*将findButton设置为窗体的默认按钮*/
 findButton->setDefault(true);

/*将findButton设为禁用,即灰色按钮*/
 findButton->setEnabled(false);
 closeButton = new QPushButton(tr("Close"));

/*当lineEdit(文本框)内容发生改变时,私有槽enableFindButton(const QString &)会被调用*/
 connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(enableFindButton(const QString &)));

/*当findButton被点击,私有槽findClicked()会被调用*/
 connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));

/*当closeButton被点击,对话框关闭,槽close()继承自QWidget,默认的功能是将控件隐藏(并非删除)*/
 connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));

/*创建布局管理器,QHBoxLayout为横向,QVBoxLayout为纵向*/
 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"));

/*设置对话框的大小,sizeHint()返回对话框的理想尺寸*/
 setFixedHeight(sizeHint().height());
}

/*当用户点击Find按钮,findClicked()槽被调用*/
void FindDialog::findClicked()
{
 QString text = lineEdit->text();

/*Qt::CaseSensitivity枚举类型,如果caseCheckBox被选择,cs为CaseSensitive(1)否则为CaseInsensitive(0)*/
 Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;

/**发射findPrevious()或findNext()信号,emit关键字会被转成标准c++
*如果前向选择按钮被选中,则发射findPrevious()信号,否则发射另一个信号
*/
 if (backwardCheckBox->isChecked()) {
  emit findPrevious(text,cs);
 } else {
  emit findNext(text,cs);
 }
}

/*当文本框内容改变时,enableFindButton槽被调用*/
void FindDialog::enableFindButton(const QString & text)
{

/*如果文本框非空,则findButton可用*/
 findButton->setEnabled(!text.isEmpty());
}


 
 "main.cpp" code:

#include <QApplication>
#include "finddialog.h"
int main(int argc,char *argv[])
{

/*创建一个QApplication对象管理整个程序资源,Qt支持命令行参数,通过构造函数传递argc和argv参数给程序使用*/
 QApplication app(argc,argv);
 FindDialog *dialog = new FindDialog;

/*将dialog设置成可见的,控件生成后一般是隐藏的*/
 dialog->show();

/*将程序的控制权交给Qt,进入事件循环*/
 return app.exec();
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值