Qt 创建 MDI 文档 window
Qt creating MDI document window
我正在尝试创建一个 MDI 文档程序。我对创建子窗口有疑问。
这是我的主窗口构造函数:
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();
}
有趣的一点是fileNew();
函数。它实际上是一个私有槽函数,我想在 "New File" 按钮被触发时调用它。这是私有插槽 fileNew()
函数:
void MainWindow::fileNew()
{
DocumentWindows* document = new DocumentWindows;
workspace->addSubWindow(document);
}
当我从主窗口构造函数调用时,这个函数工作得很好。但是,当我从使用信号槽机制的 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()));
}
即使触发SLOT也没有创建子窗口。随后,我发现如果我添加 document->show();
,一切正常。
void MainWindow::fileNew()
{
DocumentWindows* document = new DocumentWindows;
workspace->addSubWindow(document);
document->show();
}
我的问题是:为什么 SLOT 中需要 show()
函数,而构造函数中不需要?
PS。 DocumentWindows
只是一个 class 继承 QTextEdit
.
此问题与正在使用的小部件 class 无关。它与文档、MDI 或主要 window 无关。将子小部件添加到已经可见的小部件后,您必须明确 show
它。否则,小部件将保持隐藏状态。
默认情况下隐藏所有小部件。当您最初 show
MainWindow
时,它的所有子项也会递归显示。当您稍后添加子 MDI 小部件时,它会保持隐藏状态。当小部件添加到布局时,它们默认显示 - 但您的小部件由 MDI 区域管理,而不是由布局管理。
这是演示您的问题的最小测试用例:
// https://github.com/KubaO/Whosebugn/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();
}
我正在尝试创建一个 MDI 文档程序。我对创建子窗口有疑问。
这是我的主窗口构造函数:
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();
}
有趣的一点是fileNew();
函数。它实际上是一个私有槽函数,我想在 "New File" 按钮被触发时调用它。这是私有插槽 fileNew()
函数:
void MainWindow::fileNew()
{
DocumentWindows* document = new DocumentWindows;
workspace->addSubWindow(document);
}
当我从主窗口构造函数调用时,这个函数工作得很好。但是,当我从使用信号槽机制的 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()));
}
即使触发SLOT也没有创建子窗口。随后,我发现如果我添加 document->show();
,一切正常。
void MainWindow::fileNew()
{
DocumentWindows* document = new DocumentWindows;
workspace->addSubWindow(document);
document->show();
}
我的问题是:为什么 SLOT 中需要 show()
函数,而构造函数中不需要?
PS。 DocumentWindows
只是一个 class 继承 QTextEdit
.
此问题与正在使用的小部件 class 无关。它与文档、MDI 或主要 window 无关。将子小部件添加到已经可见的小部件后,您必须明确 show
它。否则,小部件将保持隐藏状态。
默认情况下隐藏所有小部件。当您最初 show
MainWindow
时,它的所有子项也会递归显示。当您稍后添加子 MDI 小部件时,它会保持隐藏状态。当小部件添加到布局时,它们默认显示 - 但您的小部件由 MDI 区域管理,而不是由布局管理。
这是演示您的问题的最小测试用例:
// https://github.com/KubaO/Whosebugn/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();
}