[英]Qt creating MDI document window
I am trying to create a MDI document program.我正在尝试创建一个 MDI 文档程序。 I have a question on creating the subwindow.我有一个关于创建子窗口的问题。
This is my mainwindow constructor:这是我的主窗口构造函数:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowTitle(tr("MDI"));
workspace = new QMdiArea;
setCentralWidget(workspace);
//fileNew();
createActions();
createMenus();
createToolbars();
statusBar()->showMessage(tr("Done"));
enableActions();
}
The interesting point is the fileNew();
有趣的一点是fileNew();
function.功能。 It is a private slot function actually which I want to invoke when "New File" button is triggered.它实际上是一个私有插槽函数,我想在触发“新建文件”按钮时调用它。 Here is the private slot fileNew()
function:这是私有插槽fileNew()
函数:
void MainWindow::fileNew()
{
DocumentWindows* document = new DocumentWindows;
workspace->addSubWindow(document);
}
This function works perfectly when I call from the mainwindow constructor.当我从 mainwindow 构造函数调用时,这个函数工作得很好。 However, there is a problem when I call it from the createActions();
但是,当我从createActions();
调用它时出现问题createActions();
function which uses a signal-slot mechanism.使用信号槽机制的函数。
Here is my createActions()
这是我的createActions()
void MainWindow::createActions()
{
newAction = new QAction(QIcon(":/Image/NewFile.png"),tr("&New"),this);
newAction->setShortcut(tr("Ctrl+N"));
newAction->setToolTip(tr("Open new document"));
connect(newAction, SIGNAL(triggered(bool)), this, SLOT(fileNew()));
}
No subwindow is created even the SLOT is triggered.即使触发了 SLOT,也不会创建子窗口。 Subsequently, I find out that if I add document->show();
随后,我发现如果我添加document->show();
, everything works well. ,一切正常。
void MainWindow::fileNew()
{
DocumentWindows* document = new DocumentWindows;
workspace->addSubWindow(document);
document->show();
}
My question is: Why the show()
function is needed in a SLOT but not in the constructor?我的问题是:为什么在 SLOT 中需要show()
函数,而在构造函数中不需要?
PS.附注。 DocumentWindows
is just a class inherits QTextEdit
. DocumentWindows
只是一个继承QTextEdit
的类。
This problem has nothing to do with the class of the widgets one is using.这个问题与正在使用的小部件的类无关。 It is unrelated to documents, MDI, or the main window.它与文档、MDI 或主窗口无关。 After you add a child widget to a widget that is already visible, you must explicitly show
it.将子小部件添加到已经可见的小部件后,您必须明确show
它。 Otherwise, the widget will remain hidden.否则,小部件将保持隐藏状态。
All widgets are hidden by default.默认情况下,所有小部件都是隐藏的。 When you initially show
the MainWindow
, all of its children are recursively shown too.当您最初show
MainWindow
,它的所有子项也会递归显示。 When you later add a child MDI widget, it remains hidden.当您稍后添加子 MDI 小部件时,它会保持隐藏状态。 When widgets are added to layouts, they are shown by default - but your widget is managed by the MDI area, not by a layout.当小部件添加到布局时,它们默认显示 - 但您的小部件由 MDI 区域管理,而不是由布局管理。
This is a minimal test case demonstrating your issue:这是展示您的问题的最小测试用例:
// https://github.com/KubaO/stackoverflown/tree/master/questions/widget-show-32534931
#include <QtWidgets>
int main(int argc, char ** argv) {
QApplication app{argc, argv};
QWidget w;
w.setMinimumSize(200, 50);
QLabel visible{"Visible", &w};
w.show();
QLabel invisible{"Invisible", &w};
invisible.move(100, 0);
return app.exec();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.