我想創建一個MDI文檔程序。我有一個關於創建子窗口的問題。Qt創建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();
功能。這實際上是一個私人插槽功能,當「新建文件」按鈕被觸發時,我想調用它。這裏是私人插槽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();
}
我的問題是:爲什麼需要一個時隙中的show()
功能,但不能在構造?
PS。 DocumentWindows
只是一個繼承QTextEdit
的類。