QT学习笔记(c++ GUI编程)find对话框
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了QT学习笔记(c++ GUI编程)find对话框,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3034字,纯文字阅读大概需要5分钟。
内容图文
![QT学习笔记(c++ GUI编程)find对话框](/upload/InfoBanner/zyjiaocheng/829/be7769b69d394211b2afb4317bd1eafd.jpg)
find对话框是c++ GUI QT4编程里的内容,之前已经手敲了一遍,
现在准备加深理解。
1.需要的窗口部件
以Dialog为主窗口,需要按钮,复选框,标签,先对这先需要使用的部件进行声明
#include<QDialog>
class QPushButton;
class QLabel;
class QCheckBox;
class QLineEdit;//单行文本编辑框
2.创建find窗口的类
class Find
{
public:
Find(QWidget * parent=0);//构造函数,表示默认Find是一个父窗口
~Find();//析构函数
private:
QPushButton *FindButton;
QPushButton *closeButton;
QLabel *label;
QLineEdit *lineedit;
QCheckBox *caseCheckBox;
QCheckBox *matchcaseBox;
};
3.跟据需要的功能确定槽和信号
在这之前,我们需要了解QT::CaseSensitivity的一些知识
这是一种枚举类型,可取值,CaseSenSitive 和Caseinsensitive
比较字符串时CaseSenSitive区分大小写,而Caseinsensitive不支持大小写。
所以我们的复选框应该有一个,match case,当然在书中还有一个复选框
是向前查询的复选框。
所以第一个槽是当用户点击复选框时候的槽
还有如果用户勾选复选框,那么就需要发送信号,所以这里需要两个信号
public slots:
void findClicked();
signals:
void findNext(const QString &ts,Qt::CaseSensitivity cs);//如果用户选择,那么发射这个信号
void findPrevious(const QString &tr,Qt::CaseSensitivity cs);
4.具体实现
这一段代码主要是对窗口部件的命名工作
FindButton =new QPushButton(tr("&Find"));
closeButton =new QPushButton(tr("close "));
setWindowTitle(tr("Find"));//为主窗口创建名字
label =new QLabel(tr("Find &What"));//这里把what设为快捷键
lineedit =new QLineEdit;
label->setBuddy(lineedit);//这里把label表示为输入框的伙伴,当使用快捷键时,自动把焦点跳到对话框上
caseCheckBox= new QCheckBox(tr("search &case"));
matchcaseBox= new QCheckBox(tr("match case"));
FindButton->setDefault(true);//设置find为默认按钮
FindButton->setEnabled(false);//当输入框里没有文字时自动禁用finf按钮
这一段主要是需要实现的操作
connect(lineedit,SIGNAL(textChanged(const QString &text)),this,SLOT(buttonenable(const QString &str)));
connect(FindButton,SIGNAL(clicked()),this,SLOT(findClicked()));
connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
下面是自定义信号和槽
void Find::findClicked()
{
QString Text=lineedit->text();
Qt::CaseSensitivity cs= matchcaseBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;//检测是否选中
if(caseCheckBox->isChecked())
emit findNext(Text,cs);
else {
emit findPrevious(Text,cs);
}
}
void Find::buttonenable(const QString &str)
{
FindButton->setEnabled(!str.isEmpty());//如果不为空就使用按钮
}
最后我们就要实现布局
5.布局
QHBoxLayout :水平布局
QVBoxlayout :垂直布局
QGridLayout :格子显示布局
QHBoxLayout *toplelayout=new QHBoxLayout;
toplelayout->addWidget(label);
toplelayout->addWidget(lineedit);
QVBoxLayout *leftlayout=new QVBoxLayout;
leftlayout->addLayout(toplelayout);
leftlayout->addWidget(matchcaseBox);
leftlayout->addWidget(caseCheckBox);
QVBoxLayout *rightlayout=new QVBoxLayout;
rightlayout ->addWidget(FindButton);
rightlayout->addWidget(closeButton);
QHBoxLayout *mainlayout=new QHBoxLayout;
mainlayout->addLayout(leftlayout);
mainlayout->addLayout(rightlayout);
setLayout(mainlayout);//使用mainlayout布局
setFixedHeight(sizeHint().height());//setFixedHight把最小和最大尺寸都设为s防止窗口不断变大或变小
运行结果如下
内容总结
以上是互联网集市为您收集整理的QT学习笔记(c++ GUI编程)find对话框全部内容,希望文章能够帮你解决QT学习笔记(c++ GUI编程)find对话框所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。