内容表

上一话题

Positioning (QML)

下一话题

元对象系统

应用程序主窗口

Creating the application window.

主窗口类概述

这些类提供典型现代主应用程序窗口所需的一切,像:主窗口本身、菜单和工具栏、状态栏等。

PySide2.QtWidgets.QAction

QAction 类提供能被插入小部件的抽象用户界面动作。

PySide2.QtWidgets.QActionGroup

QActionGroup 类把动作分组在一起。

PySide2.QtWidgets.QWidgetAction

The QWidgetAction class extends QAction by an interface for inserting custom widgets into action based containers, such as toolbars.

PySide2.QtWidgets.QDockWidget

The QDockWidget class provides a widget that can be docked inside a QMainWindow or floated as a top-level window on the desktop.

PySide2.QtWidgets.QMainWindow

QMainWindow 类提供主应用程序窗口。

PySide2.QtWidgets.QMdiArea

QMdiArea 小部件提供在其中显示 MDI 窗口的区域。

PySide2.QtWidgets.QMdiSubWindow

The QMdiSubWindow class provides a subwindow class for QMdiArea.

PySide2.QtWidgets.QMenu

QMenu 类提供用于菜单栏、上下文菜单及其它弹出菜单的菜单 Widget。

PySide2.QtWidgets.QMenuBar

QMenuBar 类提供水平菜单栏。

PySide2.QtWidgets.QSizeGrip

QSizeGrip 类提供用于重置顶层窗口尺寸的重置尺寸手柄。

PySide2.QtWidgets.QStatusBar

QStatusBar 类提供适合呈现状态信息的水平条。

PySide2.QtWidgets.QToolBar

QToolBar 类提供包含一组控件的可移动面板。

主窗口类

Qt 提供以下类为管理主窗口和关联用户界面组件:

  • QMainWindow is the central class around which applications can be built. Along with the companion QDockWidget and QToolBar classes, it represents the top-level user interface of the application.

  • QDockWidget provides a widget that can be used to create detachable tool palettes or helper windows. Dock widgets keep track of their own properties, and they can be moved, closed, and floated as external windows.

  • QToolBar provides a generic toolbar widget that can hold a number of different action-related widgets, such as buttons, drop-down menus, comboboxes, and spin boxes. The emphasis on a unified action model in Qt means that toolbars cooperate well with menus and keyboard shortcuts.

范例代码

使用 QMainWindow is straightforward. Generally, we subclass QMainWindow and set up menus, toolbars, and dock widgets inside the QMainWindow 构造函数。

To add a menu bar to the main window, we simply create the menus, and add them to the main window’s menu bar. Note that the menuBar() function will automatically create the menu bar the first time it is called. You can also call setMenuBar() to use a custom menu bar in the main window.

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    ...
    Act = new QAction(tr("&New"), self)
    Act.setShortcuts(QKeySequence.New)
    Act.setStatusTip(tr("Create a new file"))
    Act.triggered.connect(newFile)
    openAct =  QAction(tr("&Open..."), self)
    openAct.setShortcuts(QKeySequence.Open)
    openAct.setStatusTip(tr("Open an existing file"))
    openAct.triggered.connect(open)
    ...
											

Once actions have been created, we can add them to the main window components. To begin with, we add them to the pop-up menus:

fileMenu = menuBar().addMenu(tr("&File"))
fileMenu.addAction(Act)
fileMenu.addAction(openAct)
...
fileMenu.addSeparator()
...
											

QToolBar and QMenu classes use Qt’s action system to provide a consistent API. In the above code, some existing actions were added to the file menu with the addAction() 函数。 QToolBar also provides this function, making it easy to reuse actions in different parts of the main window. This avoids unnecessary duplication of work.

We create a toolbar as a child of the main window, and add the desired actions to it:

fileToolBar = addToolBar(tr("File"));
fileToolBar->addAction(newAct);
fileToolBar->addAction(openAct);
    ...
fileToolbar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
addToolBar(Qt::TopToolBarArea, fileToolbar);
											
    ...
fileToolbar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
addToolBar(Qt::TopToolBarArea, fileToolbar);
											

In this example, the toolbar is restricted to the top and bottom toolbar areas of the main window, and is initially placed in the top tool bar area. We can see that the actions specified by newAct and openAct will be displayed both on the toolbar and in the file menu.

QDockWidget is used in a similar way to QToolBar . We create a dock widget as a child of the main window, and add widgets as children of the dock widget:

contentsWindow = new QDockWidget(tr("Table of Contents"), this);
contentsWindow->setAllowedAreas(Qt::LeftDockWidgetArea
                              | Qt::RightDockWidgetArea);
addDockWidget(Qt::LeftDockWidgetArea, contentsWindow);
headingList = new QListWidget(contentsWindow);
contentsWindow->setWidget(headingList);
											

In this example, the dock widget can only be placed in the left and right dock areas, and it is initially placed in the left dock area.

QMainWindow API allows the programmer to customize which dock widget areas occupy the four corners of the dock widget area. If required, the default can be changed with the setCorner() 函数:

setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
											

The following diagram shows the configuration produced by the above code. Note that the left and right dock widgets will occupy the top and bottom corners of the main window in this layout.

../_images/mainwindow-docks-example.png

Once all of the main window components have been set up, the central widget is created and installed by using code similar to the following:

QWidget *centralWidget = new QWidget(this);
setCentralWidget(centralWidget);
											

中心 Widget 可以是任何子类化的 QWidget .