第六章 多文档界面

来源:互联网 发布:php接口开发视频教程 编辑:程序博客网 时间:2024/05/17 06:44

    在主窗口的中央区域能够提供多个文档的那些应用程序就称为多文档界面(MDI)。Qt中把QMdiArea类作为中央窗口部件,并且每一个文档窗口都是这个类的子窗口。

    对于多文档界面应用程序有一个管理,就是为他提供一个Window菜单,这个菜单中包含一些管理这个窗口以及窗口列表的命令。激活窗口会使用一个选择标记表示出来。用户在window菜单中单击代表特定窗口的一项,就可以激活任何窗口。


界面元素如下;



代码大部分与Spreadsheet相似,所以只粘贴比较重要的,工程在底下上传。


解释在注释中

MainWindow::MainWindow(){    mdiArea = new QMdiArea;//创建一个QMdiArea并设置为中央窗口部件    setCentralWidget(mdiArea);//把信号与将要用来保持更新Window菜单的槽连接起来,并且会根据应用程序的状态来启用或禁用那些动作    connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindow*)), this, SLOT(updateActions()));    createActions();    createMenus();    createToolBars();    createStatusBar();    setWindowIcon(QPixmap("images/icon.png"));    setWindowTitle(tr("MDI Editor"));    QTimer::singleShot(0, this, SLOT(loadFiles()));}

void MainWindow::updateActions(){    bool hasEditor = (activeEditor() != 0);    bool hasSelection = activeEditor()                        && activeEditor()->textCursor().hasSelection();    saveAction->setEnabled(hasEditor);    saveAsAction->setEnabled(hasEditor);    cutAction->setEnabled(hasSelection);    copyAction->setEnabled(hasSelection);    pasteAction->setEnabled(hasEditor);    closeAction->setEnabled(hasEditor);    closeAllAction->setEnabled(hasEditor);    tileAction->setEnabled(hasEditor);    cascadeAction->setEnabled(hasEditor);    nextAction->setEnabled(hasEditor);    previousAction->setEnabled(hasEditor);    separatorAction->setVisible(hasEditor);    if (activeEditor())        activeEditor()->windowMenuAction()->setChecked(true);}
    每当一个新的子窗口被激活,或者在关闭最后一个子窗口时,都会发射subWindowActivated信号。在后一种状况下,他的参数就是一个空指针。信号会连接到updateActions槽。只有在存在激活窗口时,绝大多数的菜单项才会起作用。

工程代码链接:http://pan.baidu.com/s/1jIMOVjC 密码:yxj8