如何创建第二个QT .ui表单?

问题描述 投票:0回答:2

嗯,我想要的是制作 2 个窗口应用程序( 1 个窗口有一个按钮,可以在前一个窗口中打开第二个窗口)

到目前为止我已经得到了这个:

  1. 登录.h
  2. 登录.cpp
  3. 登录.ui
  4. mainwindow.h(这里肯定没有错误,这就是我没有附加它的原因)
  5. mainwindow.cpp(这里肯定没有错误,这就是我没有附加它的原因)
  6. mainwindow.ui(这里肯定没有错误,这就是我没有附上这个的原因)

登录.h

class MainWindow2: public QWidget
{
    Q_OBJECT
public:
    explicit MainWindow2(QWidget *parent = nullptr);
    ~MainWindow2();

private:
    MainWindow2 *ui;
};

登录.cpp

MainWindow2::MainWindow2(QWidget *parent) : QWidget(parent), ui(new MainWindow2)
{

    ui->setupUi(this);
}

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

确切地说,我在login.cpp中遇到错误,错误是“MainWindow2中没有名为setupUi的成员”

.pro file


    #-------------------------------------------------
#
# Project created by QtCreator 2019-06-21T16:44:15
#
#-------------------------------------------------

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = untitled3
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

CONFIG += c++11

SOURCES += \
        login.cpp \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        login.h \
        mainwindow.h

FORMS += \
        login.ui \
        mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

c++ qt user-interface
2个回答
0
投票

仅创建唯一的QT表单设计器(QT设计器未创建.h文件)而导致“”class”中没有名为setupUi的成员”或“分配不完整类型”UI::class“”等错误,仅 .ui )。您要做的事情如下:创建QT设计器的表单类以创建.h和.ui文件。并且不要忘记按照以下方式#include 它:例如,#include“ui_mainwindow.h”。快乐编码!


0
投票

在 Qt Creator 5.8 中,首先您需要将表单添加到您的项目(*.pro)中:

add new form to Qt

然后,在第一个表单的标题中包含第二个表单的标题。然后就可以在第一种形式中声明并初始化第二种形式的指针类型了:

Form2 m_pSecondWnd = new Form 

单击按钮后,在第一种形式中创建一个槽:

this->close();
m_pSecondWnd ->show();
© www.soinside.com 2019 - 2024. All rights reserved.