QT5在VS2012中使用Qt Designer創建的UI文件與類相關聯的問題

        在MFC中,新建了一個資源對話框後,只需要Ctrl+W後,可以生成與該對話框相關聯的類。但是在VS2012+QT5的組合開發中,在添加了一個新的ui文件後,如何生成一個類與之對應呢?

1,在Qt5中的QtDesigner中生成了一個基於QDialog或者QWidget的界面ui中,編譯後,就出現了相應的一個ui_文件名.h的文件,以這userinforDlg.ui爲例,簡單介紹一下:在Qt Designer設計器下生成了這樣的一個ui文件。

這個ui生成的頭文件爲ui_userinforDlg.h

/********************************************************************************
** Form generated from reading UI file 'userinforDlg.ui'
**
** Created by: Qt User Interface Compiler version 5.2.1
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/

#ifndef UI_USERINFORDLG_H
#define UI_USERINFORDLG_H

#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QSpacerItem>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>

QT_BEGIN_NAMESPACE

class Ui_UserInforDialog
{
public:
    QWidget *layoutWidget;
    QHBoxLayout *hboxLayout;
    QSpacerItem *spacerItem;
    QPushButton *okButton;
    QPushButton *cancelButton;
    QWidget *widget;
    QVBoxLayout *verticalLayout;
    QHBoxLayout *horizontalLayout;
    QLabel *label;
    QLineEdit *lineEditName;
    QHBoxLayout *horizontalLayout_2;
    QLabel *label_2;
    QLineEdit *lineEditAge;
    QHBoxLayout *horizontalLayout_3;
    QLabel *label_3;
    QLineEdit *lineEditSex;

    void setupUi(QDialog *UserInforDialog)
    {
        if (UserInforDialog->objectName().isEmpty())
            UserInforDialog->setObjectName(QStringLiteral("UserInforDialog"));
        UserInforDialog->resize(400, 300);
        layoutWidget = new QWidget(UserInforDialog);
        layoutWidget->setObjectName(QStringLiteral("layoutWidget"));
        layoutWidget->setGeometry(QRect(20, 250, 351, 33));
        hboxLayout = new QHBoxLayout(layoutWidget);
        hboxLayout->setSpacing(6);
        hboxLayout->setObjectName(QStringLiteral("hboxLayout"));
        hboxLayout->setContentsMargins(0, 0, 0, 0);
        spacerItem = new QSpacerItem(131, 31, QSizePolicy::Expanding, QSizePolicy::Minimum);

        hboxLayout->addItem(spacerItem);

        okButton = new QPushButton(layoutWidget);
        okButton->setObjectName(QStringLiteral("okButton"));

        hboxLayout->addWidget(okButton);

        cancelButton = new QPushButton(layoutWidget);
        cancelButton->setObjectName(QStringLiteral("cancelButton"));

        hboxLayout->addWidget(cancelButton);

        widget = new QWidget(UserInforDialog);
        widget->setObjectName(QStringLiteral("widget"));
        widget->setGeometry(QRect(40, 40, 241, 141));
        verticalLayout = new QVBoxLayout(widget);
        verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
        verticalLayout->setContentsMargins(0, 0, 0, 0);
        horizontalLayout = new QHBoxLayout();
        horizontalLayout->setObjectName(QStringLiteral("horizontalLayout"));
        label = new QLabel(widget);
        label->setObjectName(QStringLiteral("label"));

        horizontalLayout->addWidget(label);

        lineEditName = new QLineEdit(widget);
        lineEditName->setObjectName(QStringLiteral("lineEditName"));

        horizontalLayout->addWidget(lineEditName);


        verticalLayout->addLayout(horizontalLayout);

        horizontalLayout_2 = new QHBoxLayout();
        horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
        label_2 = new QLabel(widget);
        label_2->setObjectName(QStringLiteral("label_2"));

        horizontalLayout_2->addWidget(label_2);

        lineEditAge = new QLineEdit(widget);
        lineEditAge->setObjectName(QStringLiteral("lineEditAge"));

        horizontalLayout_2->addWidget(lineEditAge);


        verticalLayout->addLayout(horizontalLayout_2);

        horizontalLayout_3 = new QHBoxLayout();
        horizontalLayout_3->setObjectName(QStringLiteral("horizontalLayout_3"));
        label_3 = new QLabel(widget);
        label_3->setObjectName(QStringLiteral("label_3"));

        horizontalLayout_3->addWidget(label_3);

        lineEditSex = new QLineEdit(widget);
        lineEditSex->setObjectName(QStringLiteral("lineEditSex"));

        horizontalLayout_3->addWidget(lineEditSex);


        verticalLayout->addLayout(horizontalLayout_3);


        retranslateUi(UserInforDialog);
        QObject::connect(okButton, SIGNAL(clicked()), UserInforDialog, SLOT(accept()));
        QObject::connect(cancelButton, SIGNAL(clicked()), UserInforDialog, SLOT(reject()));

        QMetaObject::connectSlotsByName(UserInforDialog);
    } // setupUi

    void retranslateUi(QDialog *UserInforDialog)
    {
        UserInforDialog->setWindowTitle(QApplication::translate("UserInforDialog", "Dialog", 0));
        okButton->setText(QApplication::translate("UserInforDialog", "OK", 0));
        cancelButton->setText(QApplication::translate("UserInforDialog", "Cancel", 0));
        label->setText(QApplication::translate("UserInforDialog", "\345\247\223\345\220\215:", 0));
        label_2->setText(QApplication::translate("UserInforDialog", "\345\271\264\351\276\204:", 0));
        label_3->setText(QApplication::translate("UserInforDialog", "\346\200\247\345\210\253:", 0));
    } // retranslateUi

};

namespace Ui {
    class UserInforDialog: public Ui_UserInforDialog {};
} // namespace Ui

QT_END_NAMESPACE

#endif // UI_USERINFORDLG_H

其實這個頭文件是Qt Designer自動生成的,我們在設計好了ui後,編譯一下,這個文件就生成了。

2.我們需要手動添加一個類與這個ui關聯。

#ifndef MYUSERINFORDLG_H
#define MYUSERINFORDLG_H

#include <QDialog>
#include "ui_userinforDlg.h"
namespace Ui
{
 class UserInforDialog;
};
class MyUserInforDlg : public QDialog
{
 Q_OBJECT

public:
 MyUserInforDlg(QWidget *parent);
 ~MyUserInforDlg();

private:
 Ui::UserInforDialog *ui;
};

#endif // MYUSERINFORDLG_H

以上是myuserinfordlg.h文件

#include "myuserinfordlg.h"

MyUserInforDlg::MyUserInforDlg(QWidget *parent)
 : QDialog(parent), ui(new Ui::UserInforDialog)
{
 ui->setupUi(this);
}

MyUserInforDlg::~MyUserInforDlg()
{
 delete ui;
}

以上是myuserinfordlg.cpp文件

這樣就把ui與類關聯起來了。

當然以上的這個類可以使用繼承。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章