第二章 创建对话框 2.3快速设计对话框
2014-04-21 20:31
330 查看
这一小节利用Qt Designer提供的可视化的设计能力,快速设计自己的窗口。
将创建一个gotocell的对话框,
按照书上的步骤,我用Qt designer生成了一个 .ui文件,我安装的是Qt 4.8.5,用的是ide 环境 Qt creator,可以新建巍项目让把ui文件直接放进项目里使用,但我发现编译之后ui文件并没有生成.h文件,所以我上网找了,后来发现可行的方法是利用qt安装目录里qmake文件夹里的uic.exe ,在命令行窗口下调用uic。 输入的命令为
uic -o ××.h ××.ui
uic和ui文件必须在一个目录下,然后会生成h文件。
ui_gotocelldialog.h如下
通过查看生成的h文件,我们可以发现生成的类没有任何基类,它存储了窗体的子窗口部件和子布局,只有一个初始化窗体的setupUi()函数,当main.cpp使用它的时候可以创建一个QDialog对象,然后传递给 setupUi函数,
main.cpp如下:
最简捷的方法就是写一个新类同时从Dialog和 Ui_GoTocellDialog继承出来,由它它来实现缺失的功能。
(通过简单的增加另外一个间接层就可以解决软件的任何问题)
新类命名惯例是去掉Ui_ 前缀
新类如下:
gotocelldialog.h
gotocelldialog.cpp实现文件:
将创建一个gotocell的对话框,
按照书上的步骤,我用Qt designer生成了一个 .ui文件,我安装的是Qt 4.8.5,用的是ide 环境 Qt creator,可以新建巍项目让把ui文件直接放进项目里使用,但我发现编译之后ui文件并没有生成.h文件,所以我上网找了,后来发现可行的方法是利用qt安装目录里qmake文件夹里的uic.exe ,在命令行窗口下调用uic。 输入的命令为
uic -o ××.h ××.ui
uic和ui文件必须在一个目录下,然后会生成h文件。
ui_gotocelldialog.h如下
/******************************************************************************** ** Form generated from reading UI file 'gotocelldialog.ui' ** ** Created by: Qt User Interface Compiler version 4.8.5 ** ** WARNING! All changes made in this file will be lost when recompiling UI file! ********************************************************************************/ #ifndef DS_H #define DS_H #include <QtCore/QVariant> #include <QtGui/QAction> #include <QtGui/QApplication> #include <QtGui/QButtonGroup> #include <QtGui/QDialog> #include <QtGui/QHBoxLayout> #include <QtGui/QHeaderView> #include <QtGui/QLabel> #include <QtGui/QLineEdit> #include <QtGui/QPushButton> #include <QtGui/QSpacerItem> #include <QtGui/QVBoxLayout> QT_BEGIN_NAMESPACE class Ui_GoTocellDialog { public: QVBoxLayout *verticalLayout; QHBoxLayout *horizontalLayout; QLabel *label; QLineEdit *lineEdit; QHBoxLayout *horizontalLayout_2; QSpacerItem *horizontalSpacer; QPushButton *cancelButton; QPushButton *okButton; void setupUi(QDialog *GoTocellDialog) { if (GoTocellDialog->objectName().isEmpty()) GoTocellDialog->setObjectName(QString::fromUtf8("GoTocellDialog")); GoTocellDialog->resize(243, 100); verticalLayout = new QVBoxLayout(GoTocellDialog); verticalLayout->setObjectName(QString::fromUtf8("verticalLayout")); horizontalLayout = new QHBoxLayout(); horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout")); label = new QLabel(GoTocellDialog); label->setObjectName(QString::fromUtf8("label")); horizontalLayout->addWidget(label); lineEdit = new QLineEdit(GoTocellDialog); lineEdit->setObjectName(QString::fromUtf8("lineEdit")); horizontalLayout->addWidget(lineEdit); verticalLayout->addLayout(horizontalLayout); horizontalLayout_2 = new QHBoxLayout(); horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2")); horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); horizontalLayout_2->addItem(horizontalSpacer); cancelButton = new QPushButton(GoTocellDialog); cancelButton->setObjectName(QString::fromUtf8("cancelButton")); horizontalLayout_2->addWidget(cancelButton); okButton = new QPushButton(GoTocellDialog); okButton->setObjectName(QString::fromUtf8("okButton")); okButton->setEnabled(false); okButton->setDefault(true); horizontalLayout_2->addWidget(okButton); verticalLayout->addLayout(horizontalLayout_2); #ifndef QT_NO_SHORTCUT label->setBuddy(lineEdit); #endif // QT_NO_SHORTCUT QWidget::setTabOrder(lineEdit, cancelButton); QWidget::setTabOrder(cancelButton, okButton); retranslateUi(GoTocellDialog); QMetaObject::connectSlotsByName(GoTocellDialog); } // setupUi void retranslateUi(QDialog *GoTocellDialog) { GoTocellDialog->setWindowTitle(QApplication::translate("GoTocellDialog", "Go to Cell", 0, QApplication::UnicodeUTF8)); label->setText(QApplication::translate("GoTocellDialog", "&Cell Location:", 0, QApplication::UnicodeUTF8)); cancelButton->setText(QApplication::translate("GoTocellDialog", "Cancel", 0, QApplication::UnicodeUTF8)); okButton->setText(QApplication::translate("GoTocellDialog", "OK", 0, QApplication::UnicodeUTF8)); } // retranslateUi }; namespace Ui { class GoTocellDialog: public Ui_GoTocellDialog {}; } // namespace Ui QT_END_NAMESPACE #endif // DS_H
通过查看生成的h文件,我们可以发现生成的类没有任何基类,它存储了窗体的子窗口部件和子布局,只有一个初始化窗体的setupUi()函数,当main.cpp使用它的时候可以创建一个QDialog对象,然后传递给 setupUi函数,
main.cpp如下:
#include<QApplication> #include<QDialog> #include"ui_gotocelldialog.h" int main(int argc,char *argv[]){ QApplication app(argc,argv); Ui_GoTocellDialog ui; QDialog *dialog=new QDialog; ui.setupUi(dialog); dialog->show(); return app.exec(); }运行之后会发现 ok键总是失效,Cancel按钮什么都做不了,行编辑器接受任何文本 所以我们需要再写一些代码使得功能完善
最简捷的方法就是写一个新类同时从Dialog和 Ui_GoTocellDialog继承出来,由它它来实现缺失的功能。
(通过简单的增加另外一个间接层就可以解决软件的任何问题)
新类命名惯例是去掉Ui_ 前缀
新类如下:
gotocelldialog.h
#ifndef GOTOCELLDIALOG_H #define GOTOCELLDIALOG_H #include<QDialog> #include"ui_gotocelldialog.h" class GoToCellDialog:public QDialog,public Ui_GoTocellDialog{//public继承,可以在该对话框外访问该对话框的窗口部件 Q_OBJECT public: GoToCellDialog(QWidget *parent=0);//构造函数, private slots: void on_lineEdit_textChanged(); }; #endif // GOTOCELLDIALOG_H
gotocelldialog.cpp实现文件:
#include<QtGui> #include"gotocelldialog.h" GoToCellDialog::GoToCellDialog(QWidget *parent):QDialog(parent){ setupUi(this);//初始化窗体,传递自己,所以可以在main中直接新建对象, QRegExp regExp("[A-Za-z][0-9]{0,2}");//检验器限制输入的范围,正则表达式, lineEdit->setValidator(new QRegExpValidator(regExp,this)); connect(okButton,SIGNAL(clicked()),this,SLOT(accept()));//ok键连接QDialog的accept()槽 connect(cancelButton,SIGNAL(clicked()),this,SLOT(reject()));//cancel键连接QDialog的reject()槽 //accept()和reject()都可以关闭对话框,但是前者将对话框返回的结果设置为QDialog::Accepted值为1;而后者值设为0 //利用这个结果变量,在使用对话框的时候可以判断用户单击了哪个按钮 } //创建了用户接口之后,setupUi函数还会自动将符合on_objectName_signalName()命名惯例的任意槽与相应的 //objectName的sinalName()信号建立信号-槽关系 //ui_gotocelldialog.h里含有lineEdit //gotocelldialog.h还有on_lineEdit_textChanged()槽 //所以建立了如下连接: //concect(lineEdit,SIGNAL(textChanged),this,SLOT(on_lineEdit_textChanged())); void GoToCellDialog::on_lineEdit_textChanged(){ okButton->setEnabled(lineEdit->hasAcceptableInput());//根据行编辑器里是否还有有效的单元格位置坐标, //on_lineEdit_textChanged()槽可以启用和禁用OK键 //hasAcceptableInput()会使用在构造函数中设置的检验器来判断行编辑其里内容的有效性。 }最后实现的运行结果如图:
相关文章推荐
- Qt 第二章 创建对话框--快速设计对话框(2) 推荐
- Qt 第二章 创建对话框--快速设计对话框
- 2.3快速设计对话框(Rapid Dialog Design)
- (转)2.3快速设计对话框(Rapid Dialog Design)
- 《算法导论》读书笔记--第二章 2.3 设计算法
- Qt5:快速设计对话框 QtDesigner 的使用
- Qt 第二章 创建对话框--改变形状的对话框(一)
- 快速设计对话框2
- 第二章 创建对话框
- 第二章 创建对话框 2.2深入介绍信号和槽
- 第二章 创建对话框(信号与槽进阶)
- Qt 第二章 创建对话框--纯代码实现改变形状的对话框(二)
- QT 初阶 第二章 创建对话框(查找对话框实例)
- 第二章 创建对话框 2.1子类化QDialog(2)
- 2.3 使用Qt设计师来创建对话框
- 关卡设计快速入门_3. 创建一个新关卡
- 第二章 创建对话框
- 快速设计对话框3
- 【Qt】2.3 使用Qt设计师来创建对话框
- Qt:快速设计对话框. C++ GUI