简体   繁体   中英

How to create second QT .ui form?

Well, what I want is make 2 windows application ( 1 window has a button that open second window in the previous window)

So far I've got this:

  1. login.h
  2. login.cpp
  3. login.ui
  4. mainwindow.h ( no errors here for sure, that's why I did not attach this)
  5. mainwindow.cpp ( no errors here for sure, that's why I did not attach this)
  6. mainwindow.ui ( no errors here for sure, that's why I did not attach this)

login.h

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

private:
    MainWindow2 *ui;
};

login.cpp

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

    ui->setupUi(this);
}

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

To be exact I've got an error in the login.cpp, the error is "no member named setupUi in MainWindow2"

.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

In Qt Creator 5.8 at first you need to add form into your project (*.pro): add new form to Qt then in first form header include second form header. Then you can declare and initialize pointer type of second form in first form

Form2 m_pSecondWnd = new Form 

and on button click slot in first form you can just call:

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

Such errors as "no member named setupUi in "class" " or "allocation of incomplete type "UI::class" " caused by creating only the only QT form designer ( QT designer did not create .h file, only .ui ). The thing you have do is follow: create class of form of QT designer to make .h and .ui file be created. And don't forget to #include it the follow way: for example, #include "ui_mainwindow.h". Happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM