[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
原创粉丝点击