我在Qt Ctreator中创建了一个包含5个窗口的MDI区域的ui文件。如您所见,在打开主窗口之后,所有子窗口也都将出现。我想先隐藏或关闭所有这些,然后再单击工具栏按钮以显示所需的窗口。但是我无法成功,为什么哪一个是错误的?
main.cpp
#include "mymainwindow.h"
#include <QApplication>
#include <QMenu>
#include <QMdiArea>
#include <QMdiSubWindow>
#include <QMainWindow>
#include <iostream>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyMainWindow w;
w.show();
return a.exec();
}
mymainwindow.cpp
#include "mymainwindow.h"
#include "ui_mymainwindow.h"
#include "iostream"
#include "QMdiSubWindow"
MyMainWindow::MyMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyMainWindow)
{
ui->setupUi(this);
//this->ui->mdiArea->closeAllSubWindows();
ui->mdiArea->hide();
}
MyMainWindow::~MyMainWindow()
{
setAttribute(Qt::WA_DeleteOnClose);
delete ui;
}
void MyMainWindow::on_tool_btn_action_Cari_triggered()
{
std::cout<<"Toolbar click is working!"<<std::endl;
statusBar()->showMessage(tr("Cari Window"));
ui->mdiArea->addSubWindow(ui->subwin_Cari);
ui->subwin_Cari->activateWindow();
ui->subwin_Cari->show();
}
mymainwindow.h
#ifndef MYMAINWINDOW_H
#define MYMAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MyMainWindow;
}
class MyMainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MyMainWindow(QWidget *parent = nullptr);
~MyMainWindow();
//private slots:
// void on_pushButton_clicked();
private slots:
void on_tool_btn_action_Cari_triggered();
private:
Ui::MyMainWindow *ui;
};
#endif // MYMAINWINDOW_H
Test14.pro
#-------------------------------------------------
#
# Project created by QtCreator 2018-07-30T23:11:33
#
#-------------------------------------------------
QT += core gui widgets
TARGET = Test14
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
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += \
main.cpp \
mymainwindow.cpp \
HEADERS += \
mymainwindow.h \
FORMS += \
mymainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
resources.qrc