一、查找对话框
1、查找对话框是应用程序中的 常用部件
(1)、目标:开发一个可以 在不同项目间复用的查找对话框
2、查找对话框的 需求分析
(1)、 可复用软件部件
(2)、查找文本框中 指定字符串
(3)、能够 指定查找方向
(4)、 支持大小写敏感查找
(5)、 附加:点击关闭按钮后隐藏

3、查找对话框的架构与设计

4、查找对话框的界面布局

#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QRadioButton>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QGridLayout>
class FindDialog : public QDialog
{
Q_OBJECT
protected:
QGridLayout m_gLayout;//注意要把声明放在组件之前,否则程序崩溃
QGroupBox m_gBox;
QHBoxLayout m_hLayout;
QLabel m_findLabel;
QLineEdit m_findLineEdit;
QPushButton m_findButton;
QCheckBox m_checkBox;
QRadioButton m_forwardButton;
QRadioButton m_backwardButton;
QPushButton m_cancleButton;
public:
explicit FindDialog(QWidget *parent = 0);
bool event(QEvent* e);
};
#endif // FINDDIALOG_H
#include "FindDialog.h"
#include <QEvent>
FindDialog::FindDialog(QWidget *parent) : QDialog(parent, Qt::WindowCloseButtonHint | Qt::Drawer)
{
setWindowTitle("查找");
m_findLabel.setText("查找内容: ");
m_findButton.setEnabled(false);
m_findButton.setText("查找下一个");
m_checkBox.setText("区分大小写");
m_forwardButton.setText("向上");
m_backwardButton.setChecked(true);
m_backwardButton.setText("向下");
m_cancleButton.setText("取消");
m_gBox.setTitle("方向");
m_hLayout.addWidget(&m_forwardButton);
m_hLayout.addWidget(&m_backwardButton);
m_gBox.setLayout(&m_hLayout);
m_gLayout.setSpacing(10);
m_gLayout.addWidget(&m_findLabel, 0, 0);
m_gLayout.addWidget(&m_findLineEdit, 0, 1);
m_gLayout.addWidget(&m_findButton, 0, 2);
m_gLayout.addWidget(&m_checkBox, 1, 0);
m_gLayout.addWidget(&m_gBox, 1, 1);
m_gLayout.addWidget(&m_cancleButton, 1, 2);
setLayout(&m_gLayout);
}
bool FindDialog::event(QEvent* e)
{
if(e->type()==QEvent::Close)
{
hide();
return true;
}
return QDialog::event(e);
}
文本编辑器其它修改的地方
头文件:

UI文件:


Slots.cpp

二、小结
(1)、查找对话框可以用作一个 可复用的软件部件进行开发
(2)、查找对话框 继承自QDialog
(3)、查找对话框的界面 通过布局管理器相互嵌套完成
(4)、查找对话框的设计与实现 是GUI学习中的经典范例
【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】
「链接」