C++ GUI Qt4学习笔记(二) 创建对话框
来源:互联网 发布:java web编程技术 pdf 编辑:程序博客网 时间:2024/05/21 07:59
一.finddialog.h文件
/*
*首先,声明四个用到的类。这里所做的是前向声明,否则就会编译出错,因为编译器不知道这些类是否存在
*为了slots的定义,需要访问Dialog的组件,因此就把其中的组件定义为成员变量以便访问。
*正是因为需要这些组件,才需要对它们的类型进行前向声明。因为我们仅仅使用的是指针,并不涉及到这些
*类的函数,因此并不需要include它们的头文件(当然也可以用引入头文件的方式,不过编译速度要慢些)
*/
#ifndef FINDDIALOG_H //预定义#define FINDDIALOG_H#include <QDialog>namespace Ui {class finddialog;}class QCheckBox;class QLabel;class QLineEdit;class QPushButton;class finddialog : public QDialog{ Q_OBJECTpublic: explicit finddialog(QWidget *parent = 0); ~finddialog();signals: void findNext(const QString &str,Qt::CaseSensitivity cs); void findPrevious(const QString &str,Qt::CaseSensitivity cs);private slots: void findClicked(); void enableFindButton(const QString &text);private: QLabel *label; QLineEdit *lineEdit; QCheckBox *caseCheckBox; QCheckBox *backwardCheckBox; QPushButton *findButton; QPushButton *closeButton; Ui::finddialog *ui;};#endif // FINDDIALOG_H二.finddialog.cpp 文件
//dialog.cpp #include "dialog.h" #include "ui_dialog.h" #include <QtGui> Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) //构造函数有参数初始化列表,用来调用父类的构造函数 { ui->setupUi(this); label = new QLabel(tr("Find &what: "));//字符串中的&代表快捷键,该label的快捷键就是Alt+W //不过这个label使用了setBuddy函数,意思是当label获得焦点时(如按下Alt+W)它的焦点会自动传给它的 //buddy,也就是lineEdit,这就是伙伴的意思。。。^_^ lineEdit = new QLineEdit; label->setBuddy(lineEdit); caseCheckBox = new QCheckBox(tr("Match &case")); backwordCheckBox = new QCheckBox(tr("Search &backford")); findButton = new QPushButton(tr("&Find"));//会生成Find字符串,当按下Alt+F时,就相当于findButton被点击了 findButton->setDefault(true); findButton->setEnabled(false);//把findButton设为不可用 closeButton = new QPushButton(tr("Close")); 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())); //后面这些都是layout的使用,编写layout布局最重要的一点就是思路清楚,想清楚哪个套路,就会比较好编写 QHBoxLayout *topLeftLayout = new QHBoxLayout; topLeftLayout->addWidget(label); topLeftLayout->addWidget(lineEdit); QVBoxLayout *leftLayout = new QVBoxLayout; leftLayout->addLayout(topLeftLayout); leftLayout->addWidget(caseCheckBox); leftLayout->addWidget(backwordCheckBox); 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("Find");//设置对话框的标题 //setFixedHeight()是将对话框设置成固定的高度,其参数值sizeHint()返回“最理想”的大小,这里使用的是height()函数去到“最理想”的高度 setFixedHeight(sizeHint().height()); } Dialog::~Dialog() { delete ui; } //findClicked()函数,首先取出lineEdit的输入值,然后判断caseCheckBox是不是选中 //如果选中,就返回Qt::CaseInsensitive,否则返回Qt::CaseSensitive,用于判断是不是 //大小写敏感的查找;最后如果backwardCheckBox被选中,就emit信号findPrevious(),否则 //emit信号findNext void Dialog::findClicked() { QString text = lineEdit->text(); Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive; if (backwordCheckBox->isChecked()) { emit findPrevious(text, cs); } else { emit findNext(text, cs); } } //enableFindButton()则根据lineEdit的内容是不是变化来设置findButton是不是可以使用 void Dialog::enableFindButton(const QString &text) { findButton->setEnabled(!text.isEmpty()); }
//main.cpp #include "dialog.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog *dialog = new Dialog; dialog->show(); return a.exec(); }
结果:
当没选中case&search两个复选框未选择时,FindButton就是不可用的灰色。。。
关于布局(layout)不是很了解,所以就先根据作者的直观的图解来理解,恩,果然很好看哇^_^ ...
0 0
- C++ GUI Qt4学习笔记(二) 创建对话框
- C++ GUI Qt4学习笔记(二)
- C++ GUI Qt4学习笔记(一)
- C++ GUI Qt4学习笔记(三)
- C++ GUI Qt4学习笔记(四)
- C++ GUI Qt4学习笔记(五)
- C++ GUI Qt4学习笔记(七)
- C++ GUI Qt4学习笔记(八)
- C++ GUI Qt4学习笔记(九)
- C++ GUI Qt4学习笔记(一)
- C++ GUI Qt4编程-创建对话框
- Qt之GUI学习笔记(四)——创建对话框
- C++ Qt4 编程学习笔记(三)——对话框
- 学习笔记之Qt4内建对话框
- Unity3D 学习笔记(二) GUI
- 【Oencv2计算机视觉学习笔记(二)】使用Qt创建GUI应用
- wxWidgets学习笔记(二):使用wxFormBuilder、wxWidgets和Code::Blocks创建GUI程序
- μc/GUI学习笔记之二第一个程序
- Javascript——表单提交
- Html5 ontouchstart与ontouchend
- 对比关系型数据库,什么情况下使用非关系型数据库(mongodb)
- 网狐棋牌(八) 异步引擎 和 网狐棋牌(九) 服务引擎概览
- Google研究员Ilya Sutskever:成功训练LDNN的13点建议
- C++ GUI Qt4学习笔记(二) 创建对话框
- 常用算法之选择排序
- 网狐棋牌(十) 成也萧何败也肖何? IUnknowEx
- XML(四)dom4j解析XML
- Binary Tree Preorder Traversal
- OC基础2:一些基本概念
- Java学习笔记六(I/O流)
- Quake2 源代码的生成和运行
- 【C++探索之旅】第一部分第三课:第一个C++程序