对话框是图像用户界面的重要构成部分,它为用户提供许多选项和多种选择。我们先通过书上例子(Find对话框)手打程序熟悉下创建对话框的过程。
0.头文件 finddialog.h
#ifndef FINDDIALOG_H #define FINDDIALOG_H //防止对头文件的多重包含 #include <QDialog> #include <Qlabel> #include <Qcheckbox> #include <QLineEdit> #include <QPushButton> #include <QHBoxLayout> #include <QVBoxLayout> #include <QMessageBox> //前置声明(forward declaration) class QCheckBox; class QLabel; class QLineEdit; class QPushButton; //定义FindDialog,并让它成为QDialog的子类 class FindDialog : public QDialog { //对于所有定义了信号和槽的类,在类定义开始处的Q_OBJECT宏都是必须的 Q_OBJECT public: //构造函数:parent参数指定了它的父窗口参数 FindDialog(QWidget *parent = 0); //此处parent=0意味着该对话框没有父对象 signals: //声明当用户单击Find按钮时对话框发射的信号 void findNext(const QString &str,Qt::CaseSensitivity cs); //向后查询(search forward) : findNext() void findPrevious(const QString &str,Qt::CaseSensitivity cs); //向前查询(search backward) : findPrevious() private slots: //private段声明两个槽 void findClicked(); void enableFindButton(const QString &text); private: //对于以下私有变量,我们使用类前置类型 //因为他们都是指针,没必要在头文件中就去访问,所以此处无需这些类的完整定义。 QLabel *label; QLineEdit *lineEdit; QCheckBox *caseCheckBox; QCheckBox *backwardCheckBox; QPushButton *findButton; QPushButton *closeButton; }; #endif // FINDDIALOG_H
1.FindDialog的实现——finddialog.cpp
#include "finddialog.h" FindDialog::FindDialog(QWidget *parent) : QDialog(parent) //将parent参数传递给基类的构造函数 //子类创建前先要构建父类,此处使用父类中带参数的构造方式QDialog(parent)构造了一个父类对象。 { label = new QLabel(tr("Find &what")); //tr()函数调用是把它们翻译成其他语言的标记 lineEdit = new QLineEdit; //将行编译器设置为标签的伙伴(buddy),即作为按下标签键时的接受焦点 label->setBuddy(lineEdit); caseCheckBox = new QCheckBox(tr("Match &case")); backwardCheckBox = new QCheckBox(tr("Search &backward")); findButton = new QPushButton(tr("&Find")); findButton->setDefault(true); //将Find按钮成为对话框的默认按钮 findButton->setEnabled(false); //禁用Find按钮 closeButton = new QPushButton(tr("Close")); //QObject是FindDialog父对象之一,故可以省略connect()函数前的QObject:: //当行编辑器中的文本发生变化时,调用私有槽enableFindButton connect(lineEdit,SIGNAL(textChanged(const QString &)), this,SLOT(enableFindButton(const QString &))); //用户单击Find按钮时,会调用findClicked()私有槽 connect(findButton,SIGNAL(clicked()), this,SLOT(findClicked())); //用户单击Close按钮时,关闭对话框 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(); //伸展器,用于占据空白区域 QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addLayout(leftLayout); mainLayout->addLayout(rightLayout); setLayout(mainLayout); setWindowTitle(tr("Find")); setFixedHeight(sizeHint().height()); } //单击Find按钮时调用findClicked()槽 //根据Search backward选项决定发射findPrevious() or findNext() void FindDialog::findClicked() { QString text = lineEdit->text(); //区分向前和向后查询中字符串大小写敏感问题 Qt::CaseSensitivity cs = caseCheckBox->isChecked() ?Qt::CaseSensitive :Qt::CaseInsensitive; if(backwardCheckBox->isChecked()){ emit findPrevious(text,cs); }else{ emit findNext(text,cs); } } //如果行编辑器文本被改变则调用enableFindButton()槽。 void FindDialog::enableFindButton(const QString &text) { findButton->setEnabled(!text.isEmpty()); }
2.创建main.cpp来运行对话框
#include <QApplication> #include "finddialog.h" int main(int argc,char *argv[]) { QApplication app(argc,argv); FindDialog *dialog = new FindDialog; dialog->show(); return app.exec(); }
运行结果:
这个对话框目的只是了解对话框的构造过程,接下来我们就会去具体实现其信号-槽的功能。
(等待施工。。。)
相关推荐
C++GuiQt4编程-创建对话框实例: #include "finddialog.h" #include int main(int argc, char *argv[]) { QApplication a(argc, argv); FindDialog w; w.show(); return a.exec(); }
PyQt5基础-创建对话框,简述了PyQt5使用过程,使用QtWidgets、QPushButton类说明 目录 PyQt5基础-创建对话框 1 软件安装 2 Python安装 2 PyQt5的安装 2 设置环境变量 2 PyQt5创建按键 3 PyQt5创建widget 4 PyQt5的...
QT--标准对话框代码
学习笔记----图形界面程序Qt安装及其在2410-s上的移植 学习笔记----图形界面程序Qt安装及其在2410-s上的移植 学习笔记----图形界面程序Qt安装及其在2410-s上的移植
QT-悬浮对话框 PPT教程+代码
QT基础:31---QMessageBox消息对话框_江南、董少-CSDN博客QT基础:31---QMessageBox消息对话框原创江南、董少阅读数 1545
Qt 03_Qt对话框(三) 标准对话框 源代码
Qt学习笔记--图形视图框架很好的,步骤清楚,手把手的教你使用Qt绘制图形视图
NULL 博文链接:https://vowtree.iteye.com/blog/1450313
qt-vs-addin-1.2.5.part3
qt-sdk-win-opensource-2010.05.exe QT很经典的一个版本,包含qtdemo,可以直接看各种demo运行效果,包括qml程序, 是初学者比较好的入门版本。 上传限制分三部分: ...qt-sdk-win-opensource-2010.05.part3.rar
http://qt.nokia.com/products/qt-addons/solutions-archive/ 有JPEG2000的,有Windows中Services的,有浏览器插件的,有单应用程序的,有SOAP的,有文件加锁操作的,有属性操作的。 如果你比较节俭资源分,那么也...
Qt源码窥探(1)---创建窗口过程图解。。
QT入门到实战篇-02-对象树及坐标系
02 对话框 源代码
学习Qt的好书。。。。。 学习Qt的好书。。。。。 学习Qt的好书。。。。。 学习Qt的好书。。。。。 学习Qt的好书。。。。。 学习Qt的好书。。。。。
C++GuiQt4编程-改变形状的对话框,就像这个实例显示的那样,设计一个扩展对话框并不比一个简单对话框难:所需要的就是一个切换按钮,一些信号-槽连接,以及一个不可以改变尺寸大小的布局。
介绍如何创建Qt的对话框。对话框是程序和用户交互的桥梁,提供了程序和用户之间对话的一种方式。 很多程序都是由一个主窗口,在这个主窗口中包含一个菜单条,多个工具条,和足够多的对话框。也有些程序本身就是一个...
qt-vs-tools-msvc2015-2.1.1(直接安装即可将qt作为插件安装到VS2015软件上)亲测成功
qt-creator-linux-x86-opensource-1.3.0