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与类关联起来了。

当然以上的这个类可以使用继承。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Qt DesignerUI文件可以通过PyQt5uic模块转换为Python代码。具体步骤如下: 1. 打开Qt Designer创建并设计好UI文件。 2. 保存UI文件,例如保存为"myui.ui"。 3. 在命令行输入以下命令: pyuic5 myui.ui -o myui.py 其,pyuic5是PyQt5uic模块,myui.uiUI文件的名称,-o表示输出文件,myui.py是输出文件的名称。 4. 执行命令后,会生成一个Python文件myui.py,其包含了UI文件的所有控件和布局。 5. 在Python代码导入myui.py文件,并使用的控件和布局。 例如: ```python from PyQt5 import QtWidgets from myui import Ui_MainWindow class MyWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) ``` 这样就可以使用UI文件的控件和布局了。 ### 回答2: QT DesignerQt的官方图形用户界面设计工具,它能够让用户可以轻松地创建QT应用的UI设计。而UI文件则是在QT Designer创建的一种特殊的XML文件。它可以方便地存储用户在该工具设计的所有控件、布局、属性等信息,并且可以供用户与Python代码相互转化。 在QT Designer完成UI文件的设计之后,用户可以将该文件转换为python代码,再利用该代码在自己的应用使用。步骤如下: 1. 打开QT Designer创建需要的控件并进行布局和属性设置,完成后保存UI文件。 2. 打开cmd窗口,进入到UI文件所在的文件夹下,使用如下命令进行转换: pyuic5 -o UI文件名称.py UI文件名称.ui,“pyuic5”是一个qt自带的命令行工具,用于将UI文件转换成python代码。-o 参数用于生成的python文件的名称,可以自定义;最后的“UI文件名称.ui”是需要转换的UI文件名称。 执行以上命令,就可以得到一个python文件,这个文件包含了所有的UI界面设计和逻辑代码。此时,用户可以在自己的应用代码引用该文件,并将其视为一个模块来使用,从而方便地实现自己的UI界面。 需要注意的是,在使用UI文件转换的过程,如果用户希望与UI文件的控件进行交互,需要在python代码进行一些其他的设置和绑定操作。比如,可以使用PyQt5.QtCore.QObject.connect()方法将信号绑定到UI文件的控件上,实现控件间的联动。此外,还应该了解一些PyQt5.QtWidgets模块提供的常用控件的属性和方法,以便于在代码更好地使用UI文件。 总之,使用QT Designer生成UI文件并将其转换为python代码是一种快速而方便的UI设计方法。它可以帮助开发者节省大量的时间和精力,并可以轻松实现各种UI复杂设计。 ### 回答3: 在使用Qt Designer创建UI界面时,我们通常会将UI文件保存为以`.ui`为后缀名的文件。然而,想要在Python使用这个UI文件,就需要先将其转换成Python脚本。 Qt 提供了一个名为 `pyuic5` 的工具,可以将 UI 文件转换为 Python 代码。该工具在Qt安装目录/bin下。在命令行输入以下命令: `pyuic5 -o output.py input.ui` 其,`input.ui`代表要转换的UI文件,`output.py`代表转换后的Python脚本。 转换后的Python脚本,会自动生成一个`Ui_XXX`,XXX就是你在Qt Designer设置的窗口名。该继承于Qt的`QWidget`,其包括了UI文件所有控件的对象和信号(slot)的定义。 为了在Python使用,我们需要在我们对主窗口的代码文件导入该,并通过初始化函数来实例化它。 ```python from PyQt5 import QtCore, QtGui, QtWidgets from my_window import Ui_MainWindow class MyMainWindow(QtWidgets.QMainWindow): def __init__(self): super().__init__() # 实例化UI self.ui = Ui_MainWindow() # 设置UI界面 self.ui.setupUi(self) # 设置信号与槽函数的关联 self.ui.pushButton.clicked.connect(self.on_pushbutton_clicked) def on_pushbutton_clicked(self): print("pushbutton clicked") ``` 在上面的代码,我首先通过`from my_window import Ui_MainWindow`导入了自动生成的UI。然后在初始化函数实例化该,并调用它的`setupUi()`函数来设置UI界面。最后,我通过`clicked`信号连接了`on_pushbutton_clicked()`槽函数,用来响应按钮点击事件。 通过对UI文件的转换和代码编写,我们可以轻松地在Python使用Qt Designer创建UI界面,实现更加丰富、友好的界面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值