QMainWindow

QMainWindow class provides a main application window. 更多

Inheritance diagram of PySide2.QtWidgets.QMainWindow

概要

函数

虚函数

信号

详细描述

Qt 主窗口框架

A main window provides a framework for building an application’s user interface. Qt has QMainWindow and its 相关类 for main window management. QMainWindow has its own layout to which you can add QToolBar s, QDockWidget s, a QMenuBar ,和 QStatusBar 。布局具有可以被任何种类 Widget 占据的中心区域。见以下布局图像。

../../_images/mainwindowlayout.png

注意

不支持创建没有中心 Widget 的主窗口。必须有中心小部件,即使它只是占位符。

创建主窗口组件

中心小部件通常是标准 Qt Widget,譬如 QTextEdit QGraphicsView 。自定义 Widget 还可以用于高级应用程序。设置中心 Widget 采用 setCentralWidget() .

主窗口有 SDI (单文档界面) 或 MDI (多文档界面)。在 Qt 中创建 MDI 应用程序通过使用 QMdiArea 作为中心 Widget。

现在,我们将研究可以被添加到主窗口的每一其它 Widget。举例说明如何创建和添加它们。

创建菜单

Qt 实现菜单在 QMenu and QMainWindow keeps them in a QMenuBar . QAction 被添加到菜单,将它们显示为菜单项。

You can add new menus to the main window’s menu bar by calling menuBar() ,其返回 QMenuBar 为窗口,然后添加菜单采用 addMenu() .

QMainWindow comes with a default menu bar, but you can also set one yourself with setMenuBar() 。若希望实现自定义菜单栏 (即:不使用 QMenuBar Widget),可以设置它采用 setMenuWidget() .

以下是如何创建菜单的范例:

void MainWindow::createMenus()
{
    fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(newAct);
    fileMenu->addAction(openAct);
    fileMenu->addAction(saveAct);
												

createPopupMenu() 函数创建弹出菜单,当主窗口接收到上下文菜单事件时。默认实现生成带来自停放 Widget 和工具栏的可复选动作的菜单。可以重实现 createPopupMenu() 为自定义菜单。

创建工具栏

工具栏的实现在 QToolBar 类。把工具栏添加到主窗口采用 addToolBar() .

控制工具栏的初始位置通过把它们赋值给特定 ToolBarArea 。可以通过插入工具栏打断分割区域 - 把它想像成文本编辑中的换行符 - 采用 addToolBarBreak() or insertToolBarBreak() 。还可以限定用户放置采用 setAllowedAreas() and setMovable() .

可以检索工具栏图标的尺寸采用 iconSize() 。尺寸从属平台;可以设置固定尺寸采用 setIconSize() 。可以更改工具栏中所有工具按钮的外观采用 setToolButtonStyle() .

以下是创建工具栏的范例:

void MainWindow::createToolBars()
{
    fileToolBar = addToolBar(tr("File"));
    fileToolBar->addAction(newAct);
												

创建停放 Widget

停放 Widget 的实现在 QDockWidget 类。停放 Widget 是可以停放在主窗口中的窗口。可以把停放 Widget 添加到主窗口采用 addDockWidget() .

有 4 个停放 Widget 区域,给出通过 DockWidgetArea enum: left, right, top, and bottom. You can specify which dock widget area that should occupy the corners where the areas overlap with setCorner() . By default each area can only contain one row (vertical or horizontal) of dock widgets, but if you enable nesting with setDockNestingEnabled() , dock widgets can be added in either direction.

Two dock widgets may also be stacked on top of each other. A QTabBar is then used to select which of the widgets should be displayed.

We give an example of how to create and add dock widgets to a main window:

dockWidget = QDockWidget(tr("Dock Widget"), self)
dockWidget.setAllowedAreas(Qt.LeftDockWidgetArea |
                                       Qt.RightDockWidgetArea)
dockWidget.setWidget(dockWidgetContents)
addDockWidget(Qt.LeftDockWidgetArea, dockWidget)
												

状态栏

可以设置状态栏采用 setStatusBar() ,但仅创建一个当首次 statusBar() (which returns the main window’s status bar) is called. See QStatusBar 了解如何使用它的有关信息。

存储状态

QMainWindow can store the state of its layout with saveState() ;稍后,可以检索它采用 restoreState() 。它是存储的工具栏和停放 Widget 的位置和尺寸 (相对于主窗口的尺寸)。

class QMainWindow ( [ parent=None [ , flags=Qt.WindowFlags() ] ] )
param parent

QWidget

param flags

WindowFlags

构造 QMainWindow 采用给定 parent and the specified widget flags .

QMainWindow sets the Window flag itself, and will hence always be created as a top-level widget.

PySide2.QtWidgets.QMainWindow. DockOption

This enum contains flags that specify the docking behavior of QMainWindow .

常量

描述

QMainWindow.AnimatedDocks

等同于 animated 特性。

QMainWindow.AllowNestedDocks

等同于 dockNestingEnabled 特性。

QMainWindow.AllowTabbedDocks

The user can drop one dock widget “on top” of another. The two widgets are stacked and a tab bar appears for selecting which one is visible.

QMainWindow.ForceTabbedDocks

Each dock area contains a single stack of tabbed dock widgets. In other words, dock widgets cannot be placed next to each other in a dock area. If this option is set, has no effect.

QMainWindow.VerticalTabs

The two vertical dock areas on the sides of the main window show their tabs vertically. If this option is not set, all dock areas show their tabs at the bottom. Implies . See also setTabPosition() .

QMainWindow.GroupedDragging

When dragging the titlebar of a dock, all the tabs that are tabbed with it are going to be dragged. Implies . Does not work well if some QDockWidgets have restrictions in which area they are allowed. (This enum value was added in Qt 5.6.)

These options only control how dock widgets may be dropped in a QMainWindow . They do not re-arrange the dock widgets to conform with the specified options. For this reason they should be set before any dock widgets are added to the main window. Exceptions to this are the and options, which may be set at any time.

PySide2.QtWidgets.QMainWindow. addDockWidget ( area , dockwidget )
参数

添加给定 dockwidget 到指定 area .

PySide2.QtWidgets.QMainWindow. addDockWidget ( area , dockwidget , orientation )
参数
  • area DockWidgetArea

  • dockwidget QDockWidget

  • orientation 取向

添加 dockwidget 进给定 area in the direction specified by the orientation .

PySide2.QtWidgets.QMainWindow. addToolBar ( toolbar )
参数

toolbar QToolBar

这是重载函数。

Equivalent of calling addToolBar ( TopToolBarArea , toolbar )

PySide2.QtWidgets.QMainWindow. addToolBar ( area , toolbar )
参数

添加 toolbar into the specified area in this main window. The toolbar is placed at the end of the current tool bar block (i.e. line). If the main window already manages toolbar then it will only move the toolbar to area .

PySide2.QtWidgets.QMainWindow. addToolBar ( title )
参数

title – unicode

返回类型

QToolBar

PySide2.QtWidgets.QMainWindow. addToolBarBreak ( [ area=Qt.TopToolBarArea ] )
参数

area ToolBarArea

Adds a toolbar break to the given area after all the other objects that are present.

PySide2.QtWidgets.QMainWindow. centralWidget ( )
返回类型

QWidget

返回主窗口的中心 Widget。此函数返回零,若中心 Widget 未设置。

PySide2.QtWidgets.QMainWindow. corner ( corner )
参数

corner Corner

返回类型

DockWidgetArea

Returns the dock widget area that occupies the specified corner .

另请参阅

setCorner()

PySide2.QtWidgets.QMainWindow. createPopupMenu ( )
返回类型

QMenu

Returns a popup menu containing checkable entries for the toolbars and dock widgets present in the main window. If there are no toolbars and dock widgets present, this function returns None .

By default, this function is called by the main window when the user activates a context menu, typically by right-clicking on a toolbar or a dock widget.

If you want to create a custom popup menu, reimplement this function and return a newly-created popup menu. Ownership of the popup menu is transferred to the caller.

PySide2.QtWidgets.QMainWindow. dockOptions ( )
返回类型

DockOptions

另请参阅

setDockOptions()

PySide2.QtWidgets.QMainWindow. dockWidgetArea ( dockwidget )
参数

dockwidget QDockWidget

返回类型

DockWidgetArea

返回 DockWidgetArea for dockwidget 。若 dockwidget has not been added to the main window, this function returns Qt::NoDockWidgetArea .

另请参阅

addDockWidget() splitDockWidget() DockWidgetArea

PySide2.QtWidgets.QMainWindow. documentMode ( )
返回类型

bool

另请参阅

setDocumentMode()

PySide2.QtWidgets.QMainWindow. iconSize ( )
返回类型

QSize

另请参阅

setIconSize()

PySide2.QtWidgets.QMainWindow. iconSizeChanged ( iconSize )
参数

iconSize QSize

PySide2.QtWidgets.QMainWindow. insertToolBar ( before , toolbar )
参数

插入 toolbar into the area occupied by the before toolbar so that it appears before it. For example, in normal left-to-right layout operation, this means that toolbar will appear to the left of the toolbar specified by before in a horizontal toolbar area.

PySide2.QtWidgets.QMainWindow. insertToolBarBreak ( before )
参数

before QToolBar

Inserts a toolbar break before the toolbar specified by before .

PySide2.QtWidgets.QMainWindow. isAnimated ( )
返回类型

bool

PySide2.QtWidgets.QMainWindow. isDockNestingEnabled ( )
返回类型

bool

PySide2.QtWidgets.QMainWindow. isSeparator ( pos )
参数

pos QPoint

返回类型

bool

PySide2.QtWidgets.QMainWindow. menuBar ( )
返回类型

QMenuBar

Returns the menu bar for the main window. This function creates and returns an empty menu bar if the menu bar does not exist.

If you want all windows in a Mac application to share one menu bar, don’t use this function to create it, because the menu bar created here will have this QMainWindow as its parent. Instead, you must create a menu bar that does not have a parent, which you can then share among all the Mac windows. Create a parent-less menu bar this way:

menuBar = QMenuBar()
												

另请参阅

setMenuBar()

PySide2.QtWidgets.QMainWindow. menuWidget ( )
返回类型

QWidget

Returns the menu bar for the main window. This function returns null if a menu bar hasn’t been constructed yet.

另请参阅

setMenuWidget()

PySide2.QtWidgets.QMainWindow. removeDockWidget ( dockwidget )
参数

dockwidget QDockWidget

移除 dockwidget from the main window layout and hides it. Note that the dockwidget is not deleted.

PySide2.QtWidgets.QMainWindow. removeToolBar ( toolbar )
参数

toolbar QToolBar

移除 toolbar from the main window layout and hides it. Note that the toolbar is not deleted.

PySide2.QtWidgets.QMainWindow. removeToolBarBreak ( before )
参数

before QToolBar

Removes a toolbar break previously inserted before the toolbar specified by before .

PySide2.QtWidgets.QMainWindow. resizeDocks ( docks , sizes , orientation )
参数
  • docks

  • sizes

  • orientation 取向

Resizes the dock widgets in the list docks to the corresponding size in pixels from the list sizes 。若 orientation is Horizontal , adjusts the width, otherwise adjusts the height of the dock widgets. The sizes will be adjusted such that the maximum and the minimum sizes are respected and the QMainWindow itself will not be resized. Any additional/missing space is distributed amongst the widgets according to the relative weight of the sizes.

范例:

resizeDocks({blueWidget, yellowWidget}, {20 , 40}, Qt::Horizontal);
												

If the blue and the yellow widget are nested on the same level they will be resized such that the yellowWidget is twice as big as the blueWidget

If some widgets are grouped in tabs, only one widget per group should be specified. Widgets not in the list might be changed to repect the constraints.

PySide2.QtWidgets.QMainWindow. restoreDockWidget ( dockwidget )
参数

dockwidget QDockWidget

返回类型

bool

Restores the state of dockwidget if it is created after the call to restoreState() 。返回 true if the state was restored; otherwise returns false .

PySide2.QtWidgets.QMainWindow. restoreState ( state [ , version=0 ] )
参数
  • state QByteArray

  • version int

返回类型

bool

Restores the state of this mainwindow’s toolbars and dockwidgets. Also restores the corner settings too. The version number is compared with that stored in state . If they do not match, the mainwindow’s state is left unchanged, and this function returns false ; otherwise, the state is restored, and this function returns true .

要还原保存几何体使用 QSettings ,可以使用的代码像这样:

def readSettings(self):
    settings = QSettings("MyCompany", "MyApp")
    restoreGeometry(settings.value("myWidget/geometry"))
    restoreState(settings.value("myWidget/windowState"))
												
PySide2.QtWidgets.QMainWindow. saveState ( [ version=0 ] )
参数

version int

返回类型

QByteArray

Saves the current state of this mainwindow’s toolbars and dockwidgets. This includes the corner settings which can be set with setCorner() version number is stored as part of the data.

objectName property is used to identify each QToolBar and QDockWidget . You should make sure that this property is unique for each QToolBar and QDockWidget you add to the QMainWindow

To restore the saved state, pass the return value and version number to restoreState() .

要在窗口关闭时保存几何体,可以实现像这样的关闭事件:

def closeEvent(self, event):
    settings = QSettings("MyCompany", "MyApp")
    settings.setValue("geometry", self.saveGeometry())
    settings.setValue("windowState", self.saveState())
    QMainWindow.closeEvent(self, event)
												
PySide2.QtWidgets.QMainWindow. setAnimated ( enabled )
参数

enabled bool

另请参阅

isAnimated()

PySide2.QtWidgets.QMainWindow. setCentralWidget ( widget )
参数

widget QWidget

设置给定 widget to be the main window’s central widget.

注意: QMainWindow 拥有所有权对于 widget pointer and deletes it at the appropriate time.

另请参阅

centralWidget()

PySide2.QtWidgets.QMainWindow. setCorner ( corner , area )
参数
  • corner Corner

  • area DockWidgetArea

Sets the given dock widget area to occupy the specified corner .

另请参阅

corner()

PySide2.QtWidgets.QMainWindow. setDockNestingEnabled ( enabled )
参数

enabled bool

PySide2.QtWidgets.QMainWindow. setDockOptions ( options )
参数

options DockOptions

另请参阅

dockOptions()

PySide2.QtWidgets.QMainWindow. setDocumentMode ( enabled )
参数

enabled bool

另请参阅

documentMode()

PySide2.QtWidgets.QMainWindow. setIconSize ( iconSize )
参数

iconSize QSize

另请参阅

iconSize()

PySide2.QtWidgets.QMainWindow. setMenuBar ( menubar )
参数

menubar QMenuBar

把主窗口菜单栏设为 menuBar .

注意: QMainWindow 拥有所有权对于 menuBar pointer and deletes it at the appropriate time.

另请参阅

menuBar()

PySide2.QtWidgets.QMainWindow. setMenuWidget ( menubar )
参数

menubar QWidget

把主窗口菜单栏设为 menuBar .

QMainWindow 拥有所有权对于 menuBar pointer and deletes it at the appropriate time.

另请参阅

menuWidget()

PySide2.QtWidgets.QMainWindow. setStatusBar ( statusbar )
参数

statusbar QStatusBar

把主窗口状态栏设为 statusbar .

Setting the status bar to None will remove it from the main window. Note that QMainWindow 拥有所有权对于 statusbar pointer and deletes it at the appropriate time.

另请参阅

statusBar()

PySide2.QtWidgets.QMainWindow. setTabPosition ( areas , tabPosition )
参数
  • areas DockWidgetAreas

  • tabPosition TabPosition

Sets the tab position for the given dock widget areas 到指定 tabPosition . By default, all dock areas show their tabs at the bottom.

注意

VerticalTabs dock option overrides the tab positions set by this method.

PySide2.QtWidgets.QMainWindow. setTabShape ( tabShape )
参数

tabShape TabShape

另请参阅

tabShape()

PySide2.QtWidgets.QMainWindow. setToolButtonStyle ( toolButtonStyle )
参数

toolButtonStyle ToolButtonStyle

另请参阅

toolButtonStyle()

PySide2.QtWidgets.QMainWindow. setUnifiedTitleAndToolBarOnMac ( set )
参数

set bool

PySide2.QtWidgets.QMainWindow. splitDockWidget ( after , dockwidget , orientation )
参数

Splits the space covered by the first dock widget into two parts, moves the first dock widget into the first part, and moves the second dock widget into the second part.

orientation specifies how the space is divided: A Horizontal split places the second dock widget to the right of the first; a Vertical split places the second dock widget below the first.

注意 : if first is currently in a tabbed docked area, second will be added as a new tab, not as a neighbor of first . This is because a single tab can contain only one dock widget.

注意 : The LayoutDirection influences the order of the dock widgets in the two parts of the divided area. When right-to-left layout direction is enabled, the placing of the dock widgets will be reversed.

PySide2.QtWidgets.QMainWindow. statusBar ( )
返回类型

QStatusBar

返回用于主窗口的状态栏。此函数创建并返回空状态栏,若状态栏不存在。

另请参阅

setStatusBar()

PySide2.QtWidgets.QMainWindow. tabPosition ( area )
参数

area DockWidgetArea

返回类型

TabPosition

返回选项卡位置为 area .

注意

VerticalTabs 停放选项覆盖由此函数返回的选项卡位置。

PySide2.QtWidgets.QMainWindow. tabShape ( )
返回类型

TabShape

另请参阅

setTabShape()

PySide2.QtWidgets.QMainWindow. tabifiedDockWidgetActivated ( dockWidget )
参数

dockWidget QDockWidget

PySide2.QtWidgets.QMainWindow. tabifiedDockWidgets ( dockwidget )
参数

dockwidget QDockWidget

返回类型

Returns the dock widgets that are tabified together with dockwidget .

PySide2.QtWidgets.QMainWindow. tabifyDockWidget ( first , second )
参数

移动 second dock widget on top of first dock widget, creating a tabbed docked area in the main window.

PySide2.QtWidgets.QMainWindow. takeCentralWidget ( )
返回类型

QWidget

Removes the central widget from this main window.

The ownership of the removed widget is passed to the caller.

PySide2.QtWidgets.QMainWindow. toolBarArea ( toolbar )
参数

toolbar QToolBar

返回类型

ToolBarArea

返回 ToolBarArea for toolbar 。若 toolbar has not been added to the main window, this function returns Qt::NoToolBarArea .

另请参阅

addToolBar() addToolBarBreak() ToolBarArea

PySide2.QtWidgets.QMainWindow. toolBarBreak ( toolbar )
参数

toolbar QToolBar

返回类型

bool

Returns whether there is a toolbar break before the toolbar .

PySide2.QtWidgets.QMainWindow. toolButtonStyle ( )
返回类型

ToolButtonStyle

PySide2.QtWidgets.QMainWindow. toolButtonStyleChanged ( toolButtonStyle )
参数

toolButtonStyle ToolButtonStyle

PySide2.QtWidgets.QMainWindow. unifiedTitleAndToolBarOnMac ( )
返回类型

bool