博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QT练习4:编写一个查找对话框
阅读量:6873 次
发布时间:2019-06-26

本文共 2965 字,大约阅读时间需要 9 分钟。

分为3个文件:

finddialog.h:

 
#ifndef FINDDIALOG_H
#define
FINDDIALOG_H
#include
<
QDialog
>
class
QCheckBox;
class
QLabel;
class
QLineEdit;
class
QPushButton;
//
自定义类,继承于QDialog
class
FindDialog:
public
QDialog
{
Q_OBJECT
public
:
FindDialog(QWidget
*
parent
=
0
);
//
构造函数
signals:
void
findNext(
const
QString
&
str,
bool
caseSensitive);
void
findPrev(
const
QString
&
str,
bool
caseSensitive);
private
slots:
//
私有槽
void
findClicked();
void
enableFindButton(
const
QString
&
text);
private
:
//
声明窗口要使用到的部件
QLabel
*
label;
QLineEdit
*
lineEdit;
QCheckBox
*
caseCheckBox;
QCheckBox
*
backwardCheckBox;
QPushButton
*
findButton;
QPushButton
*
closeButton;
};
#endif
//
FINDDIALOG_H
finddialog.cpp
 
#include
<
QCheckBox
>
#include
<
QLabel
>
#include
<
QLayout
>
#include
<
QLineEdit
>
#include
<
QPushButton
>
#include
"
finddialog.h
"
FindDialog::FindDialog(QWidget
*
parent)
:QDialog(parent)
//
对父类进行构造
{
label
=
new
QLabel(tr(
"
Find &what:
"
),
this
);
lineEdit
=
new
QLineEdit(
"
wenhao
"
,
this
);
label
->
setBuddy(lineEdit);
//
label,lineEdit是伙伴关系.按label的快捷键进入lineEdit中
caseCheckBox
=
new
QCheckBox(tr(
"
Match &case
"
),
this
);
backwardCheckBox
=
new
QCheckBox(tr(
"
Search &backward
"
),
this
);
findButton
=
new
QPushButton(tr(
"
&Find
"
),
this
);
findButton
->
setDefault(
true
);
//
接回车键相当于按下findButton键
findButton
->
setEnabled(
false
);
//
这个设置让其默认是不可以点击的.
closeButton
=
new
QPushButton(tr(
"
Close
"
),
this
);
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()));
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(
1
);
//
QHBoxLayout
*
mainLayout
=
new
QHBoxLayout(
this
);
mainLayout
->
setMargin(
11
);
mainLayout
->
setSpacing(
4
);
mainLayout
->
addLayout(leftLayout);
mainLayout
->
addLayout(rightLayout);
setLayout(mainLayout);
setWindowTitle(tr(
"
Find...
"
));
}
void
FindDialog::findClicked()
{
QString text
=
lineEdit
->
text();
bool
caseSensitive
=
caseCheckBox
->
isChecked();
if
(backwardCheckBox
->
isChecked())
emit findPrev(text,caseSensitive);
else
emit findNext(text,caseSensitive);
}
void
FindDialog::enableFindButton(
const
QString
&
text)
{
findButton
->
setEnabled(
!
text.isEmpty());
}
main.cpp
 
#include
<
QApplication
>
#include
<
QPushButton
>
#include
<
QHBoxLayout
>
#include
<
QWidget
>
#include
"
finddialog.h
"
int
main(
int
argc,
char
*
argv[])
{
QApplication app(argc,argv);
FindDialog
*
dialog
=
new
FindDialog;
dialog
->
show();
return
app.exec();
}

效果图:

2011052215131038.png

转载地址:http://uypfl.baihongyu.com/

你可能感兴趣的文章
获取音视频文件AVMetadata数据
查看>>
sql serve 创建序列
查看>>
模型层的生成
查看>>
关于APP接口设计
查看>>
【VI】如何再执行上一个(历史)命令(已解决)
查看>>
KendoUI系列:DropDownList
查看>>
Axure7.0汉化方法
查看>>
我的MYSQL学习心得(九)
查看>>
JavaScript高级程序设计学习笔记--DOM
查看>>
Python易学就会(五)turtle绘制椭圆与递归
查看>>
echarts map地图设置外边框或者阴影
查看>>
使用vue-cli脚手架+webpack搭建vue项目
查看>>
Docker - 03 编排容器 Docker Compose 指令速查表
查看>>
Mybatis基本映射--INSERT
查看>>
手把手教你理解卷积神经网络
查看>>
猴子都能看懂的《Git 分支管理》
查看>>
【面试算法】链表反转
查看>>
镭速(Raysync)文件传输高可用安装部署介绍!
查看>>
使用 Jaeger 完成服务间的链路追踪
查看>>
Java NIO使用及原理分析 (一)
查看>>