我想制作一个2窗口应用程序(一个窗口有一个按钮,可以在前一个窗口中打开第二个窗口)
到目前为止,我已经得到了这个:
登录.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
在 Qt Creator 5.8 中,首先您需要将表单添加到您的项目(*.pro)中:
然后,在第一个表单的标题中包含第二个表单的标题。然后就可以在第一种形式中声明并初始化第二种形式的指针类型了:
Form2 m_pSecondWnd = new Form
单击按钮后,在第一种形式中创建一个槽:
this->close();
m_pSecondWnd ->show();
““类”中没有名为 setupUi 的成员
“不完整类型“UI::class”的分配
此类错误是由于仅创建 Qt Designer 表单而导致的(Qt Designer 未创建
.h
文件,仅创建 .ui
)。
您必须创建一个 Qt Designer 表单类,才能创建
.h
和 .ui
文件。并且不要忘记#include
它,例如,#include "ui_mainwindow.h"
。