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

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

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

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

  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 文件:

    #-------------------------------------------------
#
# 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 qt-creator qt-designer
2个回答
1
投票

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

add new form to Qt

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

Form2 m_pSecondWnd = new Form 

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

this->close();
m_pSecondWnd ->show();

0
投票

““类”中没有名为 setupUi 的成员

“不完整类型“UI::class”的分配

此类错误是由于仅创建 Qt Designer 表单而导致的(Qt Designer 未创建

.h
文件,仅创建
.ui
)。

您必须创建一个 Qt Designer 表单类,才能创建

.h
.ui
文件。并且不要忘记
#include
它,例如,
#include "ui_mainwindow.h"

© www.soinside.com 2019 - 2024. All rights reserved.