[Qt起航]第三篇——(二)对话框的设计
来源:互联网 发布:移动网络运营商是哪个 编辑:程序博客网 时间:2024/06/09 15:36
一、手动绘制对话框,生成代码
// 1.创建一个Qt Application项目:gotocelldialog// 2.点击“Qt5”-->“Launch Desiner”-->绘制需要的的窗体-->保存为“gotocelldialog.ui”// 3.重新编译、运行程序,提示“gotocelldialogClass”: 不是“Ui”的成员。查看“ui_gotocelldialog.h”,发现自动生成的窗体名为“MainWindow”// 4.Ui::gotocelldialogClassui;修改为Ui::MainWindow ui;或者在绘制窗体时,将窗体名命名为gotocelldialogClassui。编译运行,OK。// 5. ui文件 <?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>271</width> <height>79</height> </rect> </property> <property name="windowTitle"> <string>MainWindow</string> </property> <widget class="QWidget" name="centralwidget"> <widget class="QLabel" name="label"> <property name="geometry"> <rect> <x>32</x> <y>2</y> <width>48</width> <height>16</height> </rect> </property> <property name="text"> <string>GoToCell</string> </property> </widget> <widget class="QWidget" name=""> <property name="geometry"> <rect> <x>71</x> <y>2</y> <width>160</width> <height>53</height> </rect> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <widget class="QLineEdit" name="lineEdit"/> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <widget class="QPushButton" name="pushButton"> <property name="text"> <string>Ok</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pushButton_2"> <property name="text"> <string>Cancel</string> </property> </widget> </item> </layout> </item> </layout> </widget> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/></ui>
二、快速设计对话框——代码整理
// ui_gotocelldialog.h 代码#ifndef UI_GOTOCELLDIALOG_H#define UI_GOTOCELLDIALOG_H#include <QtCore/QVariant>#include <QAction>#include <QApplication>#include <QButtonGroup>#include <QDialogButtonBox>#include <QHBoxLayout>#include <QHeaderView>#include <QLabel>#include <QLineEdit>#include <QVBoxLayout>#include <QWidget>// vs2015报错:使用了未定义类型QPushButton,需要添加此头文件。实际上是头文件"qdialog.h"前置声明了,单没有加入头文件#include <QPushButton>QT_BEGIN_NAMESPACEclass Ui_GoToCellDialog{public: QVBoxLayout *vboxLayout; QHBoxLayout *hboxLayout; QLabel *label; QLineEdit *lineEdit; QDialogButtonBox *buttonBox; void setupUi(QWidget *GoToCellDialog) { if (GoToCellDialog->objectName().isEmpty()) GoToCellDialog->setObjectName(QString::fromUtf8("GoToCellDialog")); GoToCellDialog->resize(273, 83); vboxLayout = new QVBoxLayout(GoToCellDialog); vboxLayout->setObjectName(QString::fromUtf8("vboxLayout")); hboxLayout = new QHBoxLayout(); hboxLayout->setObjectName(QString::fromUtf8("hboxLayout")); label = new QLabel(GoToCellDialog); label->setObjectName(QString::fromUtf8("label")); hboxLayout->addWidget(label); lineEdit = new QLineEdit(GoToCellDialog); lineEdit->setObjectName(QString::fromUtf8("lineEdit")); hboxLayout->addWidget(lineEdit); vboxLayout->addLayout(hboxLayout); buttonBox = new QDialogButtonBox(GoToCellDialog); buttonBox->setObjectName(QString::fromUtf8("buttonBox")); buttonBox->setOrientation(Qt::Horizontal); buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::NoButton | QDialogButtonBox::Ok); vboxLayout->addWidget(buttonBox);#ifndef QT_NO_SHORTCUT label->setBuddy(lineEdit);#endif // QT_NO_SHORTCUT retranslateUi(GoToCellDialog); QMetaObject::connectSlotsByName(GoToCellDialog); } // setupUi void retranslateUi(QWidget *GoToCellDialog) { GoToCellDialog->setWindowTitle(QApplication::translate("GoToCellDialog", "Go to Cell", 0)); label->setText(QApplication::translate("GoToCellDialog", "&Cell Location:", 0)); } // retranslateUi};namespace Ui { class GoToCellDialog : public Ui_GoToCellDialog {};} // namespace UiQT_END_NAMESPACE#endif // UI_GOTOCELLDIALOG_H
// gotocelldialog.h#ifndef GOTOCELLDIALOG_H#define GOTOCELLDIALOG_H#include <QDialog>#include "ui_gotocelldialog.h"class GoToCellDialog : public QDialog, public Ui::GoToCellDialog{ Q_OBJECTpublic: GoToCellDialog(QWidget *parent = 0); private slots: void on_lineEdit_textChanged();};#endif
// gotocelldialog.cpp#include <QtGui>#include <QDialogButtonBox>#include "gotocelldialog.h"GoToCellDialog::GoToCellDialog(QWidget *parent) : QDialog(parent){ setupUi(this); buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}"); lineEdit->setValidator(new QRegExpValidator(regExp, this)); connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));}void GoToCellDialog::on_lineEdit_textChanged(){ buttonBox->button(QDialogButtonBox::Ok)->setEnabled( lineEdit->hasAcceptableInput());}
// 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();}
// 工程编译问题汇总// 一、使用了未定义类型QPushButton// qdialogh的前置声明#ifndef QDIALOG_H#define QDIALOG_H#include <QtWidgets/qwidget.h>QT_BEGIN_NAMESPACE// QPushButton 的前置声明class QPushButton;class QDialogPrivate;......//*********************************************************//// 二、moc文件不存在// 单机其他已存在的moc文件对应的头文件,选择其"属性"->“常规”的内容拷贝到提示的moc文件对应的头文件属性中//*********************************************************//// 三、QApplication没有成员UnicodeUTF8// QCoreApplication::UnicodeUTF8已被弃用,直接删除即可。所有的情况将使用UTF-8。
0 0
- [Qt起航]第三篇——(二)对话框的设计
- [QT起航] 第三篇——(一)创建对话框
- [QT起航] 第二篇——(二)再识Qt的信号与槽机制
- QT开发(二十)——QT对话框
- [QT起航] 第二篇——(一)初识Qt(第一个Qt程序、信号与槽、布局)
- JS篇——起航
- Qt之对话框设计——电子时钟
- QT对话框设计——利用QPalette改变控件颜色
- 我的嵌入式学习之旅——起航篇
- Qt下等待对话框的设计
- 八、Qt对话框设计
- QT对话框设计
- QT -- 快速设计对话框
- 编码之旅——起航篇
- Java之旅——起航篇
- 【三层转七层】——起航篇
- 扬帆——起航
- QT对话框——利用QT设计师
- 指针数组 数组指针 指针函数 函数指针的区别
- Hadoop学习笔记(11)-搭建Hive伪分布式及简单操作
- 单例模式与静态变量的区别
- java中的static
- python 面向对象基础
- [Qt起航]第三篇——(二)对话框的设计
- nodejs分析excel文件将结果写入excel文件
- flex布局
- 视频播放控件SurfaceView与VideoView
- JAVA-正则表达式
- poi操作excel
- 在两个ViewController之间传输数据
- AIDL通信机制简单使用步骤
- Python笔记--String