学习自唐佐林老师Qt
FindDialog.h
#ifndef _FINDDIALOG_H_
#define _FINDDIALOG_H_
#include <QDialog>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRadioButton>
#include <QCheckBox>
#include <QGroupBox>
#include <QPlainTextEdit>
#include <QPointer>
//查找对话框
class FindDialog : public QDialog
{
Q_OBJECT
protected:
QGroupBox m_radioGrpBx;//容器 用于放置两个单选按钮 QRadioButton
//网格布局管理器,水平布局管理器
QGridLayout m_layout;
QHBoxLayout m_hbLayout;
QLabel m_findLbl;//查找标签
QLineEdit m_findEdit;//查找文本输入框
QPushButton m_findBtn;//查找按钮 查找下一个
QPushButton m_closeBtn;//关闭按钮
QCheckBox m_matchChkBx; //复选框
QRadioButton m_forwardBtn;//单选按钮 向前查找
QRadioButton m_backwardBtn;//单选按钮 向后查找
QPointer<QPlainTextEdit> m_pText;//智能指针指向 文本框对象
void initControl();
void connectSlot();
protected slots:
void onFindClicked();//查找按钮 槽
void onCloseClicked();//关闭按钮 槽
public:
explicit FindDialog(QWidget* parent = 0, QPlainTextEdit* pText = 0);
void setPlainTextEdit(QPlainTextEdit* pText);
QPlainTextEdit* getPlainTextEdit();
bool event(QEvent* evt);//重写事件处理
};
#endif // _FINDDIALOG_H_
FindDialog.cpp
#include "FindDialog.h"
#include <QEvent>
#include <QTextCursor>
#include <QMessageBox>
/*
QDialog(parent, Qt::WindowCloseButtonHint | Qt::Drawer)
窗口选项1:Qt::WindowCloseButtonHin 只显示一个关闭按钮
窗口选项2:Qt::Drawe 当前对话框 不显示左上角图标
*/
FindDialog::FindDialog(QWidget *parent, QPlainTextEdit* pText)
: QDialog(parent, Qt::WindowCloseButtonHint | Qt::Drawer)
{
initControl();
connectSlot();
setLayout(&m_layout);
setWindowTitle("Find");
setPlainTextEdit(pText);
}
//界面布局
void FindDialog::initControl()
{
m_findLbl.setText("Find What:");
m_findBtn.setText("Find Next");
m_closeBtn.setText("Close");
m_matchChkBx.setText("Match Case");
m_backwardBtn.setText("Backward");
m_forwardBtn.setText("Forward");
m_forwardBtn.setChecked(true);//默认被选取
m_radioGrpBx.setTitle("Direction");//m_radioGrpBx容器 放置两个单选按钮 QRadioButton,设置标题
//添加到水平布局管理器
m_hbLayout.addWidget(&m_forwardBtn);
m_hbLayout.addWidget(&m_backwardBtn);
m_radioGrpBx.setLayout(&m_hbLayout);
m_layout.setSpacing(10);
m_layout.addWidget(&m_findLbl, 0, 0);
m_layout.addWidget(&m_findEdit, 0, 1);
m_layout.addWidget(&m_findBtn, 0, 2);
m_layout.addWidget(&m_matchChkBx, 1, 0);
m_layout.addWidget(&m_radioGrpBx, 1, 1);
m_layout.addWidget(&m_closeBtn, 1, 2);
}
//映射信号到槽
void FindDialog::connectSlot()
{
//映射信号 查找,关闭动作 到槽
connect(&m_findBtn, SIGNAL(clicked()), this, SLOT(onFindClicked()));
connect(&m_closeBtn, SIGNAL(clicked()), this, SLOT(onCloseClicked()));
}
void FindDialog::setPlainTextEdit(QPlainTextEdit* pText)
{
m_pText = pText;
}
QPlainTextEdit* FindDialog::getPlainTextEdit()
{
return m_pText;
}
//重写事件处理,点击关闭按钮 隐藏对话开关
bool FindDialog::event(QEvent* evt)
{
//判断有没有收到 关闭 事件
if( evt->type() == QEvent::Close )
{
hide();//隐藏当前对话框
return true; //返回true 代表当前事件已经被处理了
}
return QDialog::event(evt);//其他事件 直接交给父类事件处理函数
}
//查找按钮 槽
void FindDialog::onFindClicked()
{
QString target = m_findEdit.text();//获取需要查找的目标字符串
if( (m_pText != NULL) && (target != "") )
{
QString text = m_pText->toPlainText();//取出文本框中所有字符
QTextCursor c = m_pText->textCursor();//获取光标对象
int index = -1;//标记字符串下标
//向后查找
if( m_forwardBtn.isChecked() )//如果向前查找按钮 被选择
{
/*查找目标字符串target
从当前光标的位置开始查找 c.position
是否大小写敏感查找 m_matchChkBx.isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive
CaseSensitive 大小写敏感
*/
index = text.indexOf(target, c.position(), m_matchChkBx.isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive);
//找到了目标字符串
if( index >= 0 )
{
//从前向后查找,设置选择范围,光标属性 QTextCursor::KeepAnchor
c.setPosition(index);
c.setPosition(index + target.length(), QTextCursor::KeepAnchor);
//设置选择好的光标信息
m_pText->setTextCursor(c);
}
}
//向前查找
if( m_backwardBtn.isChecked() )
{
index = text.lastIndexOf(target, c.position() - text.length() - 1, m_matchChkBx.isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive);
if( index >= 0 )
{
c.setPosition(index + target.length());
c.setPosition(index, QTextCursor::KeepAnchor);
m_pText->setTextCursor(c);
}
}
//如果查找不到
if( index < 0 )
{
QMessageBox msg(this);//提示对话框
msg.setWindowTitle("Find");
msg.setText("Can not find \"" + target + "\" any more...");
msg.setIcon(QMessageBox::Information);
msg.setStandardButtons(QMessageBox::Ok);//对话框按钮
msg.exec();
}
}
}
//关闭按钮槽
void FindDialog::onCloseClicked()
{
close();
}
定义指向查找对话框智能指针
QSharedPointer m_pFindDlg;
//初始化智能指针
m_pFindDlg(new FindDialog(this,&mainEditor))
//打开 查找对话框
m_pFindDlg->show();