QMainWindow

概要

函数

虚函数

信号

详细描述

PySide.QtGui.QMainWindow class provides a main application window.

Qt 主窗口框架

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

../../_images/mainwindowlayout.png

注意

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

创建主窗口组件

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

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

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

创建菜单

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

可以把新菜单添加到主窗口的菜单栏通过调用 menuBar() ,其返回 PySide.QtGui.QMenuBar 为窗口,然后添加菜单采用 QMenuBar.addMenu() .

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

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

def createMenus(self):
    fileMenu = menuBar().addMenu(tr("&File"))
    fileMenu.addAction(Act)
    fileMenu.addAction(openAct)
    fileMenu.addAction(saveAct)
											

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

创建工具栏

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

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

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

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

def createToolBars(self):
    fileToolBar = addToolBar(tr("File"))
    fileToolBar.addAction(Act)
											

创建停放 Widget

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

有 4 个停放 Widget 区域,给出通过 Qt.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 PySide.QtGui.QTabBar is then used to select which of the widgets that 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() (返回主窗口的状态栏) 被调用。见 PySide.QtGui.QStatusBar 了解如何使用它的有关信息。

存储状态

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

另请参阅

PySide.QtGui.QMenuBar PySide.QtGui.QToolBar PySide.QtGui.QStatusBar PySide.QtGui.QDockWidget 应用程序范例 停放 Widget 范例 MDI 范例 SDI 范例 菜单范例

class PySide.QtGui. QMainWindow ( [ parent=None [ , flags=0 ] ] )
参数:
PySide.QtGui.QMainWindow. DockOption

This enum contains flags that specify the docking behavior of PySide.QtGui.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, AllowNestedDocks 不起作用。
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 AllowTabbedDocks 。另请参阅 PySide.QtGui.QMainWindow.setTabPosition() .

These options only control how dock widgets may be dropped in a PySide.QtGui.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 AnimatedDocks and VerticalTabs options, which may be set at any time.

PySide.QtGui.QMainWindow. addDockWidget ( area , dockwidget , orientation )
参数:
PySide.QtGui.QMainWindow. addDockWidget ( area , dockwidget )
参数:
PySide.QtGui.QMainWindow. addToolBar ( area , toolbar )
参数:
PySide.QtGui.QMainWindow. addToolBar ( title )
参数: title – unicode
返回类型: PySide.QtGui.QToolBar

这是重载函数。

创建 PySide.QtGui.QToolBar object, setting its window title to title , and inserts it into the top toolbar area.

PySide.QtGui.QMainWindow. addToolBar ( toolbar )
参数: toolbar PySide.QtGui.QToolBar

这是重载函数。

Equivalent of calling addToolBar( Qt.TopToolBarArea , toolbar )

PySide.QtGui.QMainWindow. addToolBarBreak ( [ area=Qt.TopToolBarArea ] )
参数: area PySide.QtCore.Qt.ToolBarArea
PySide.QtGui.QMainWindow. centralWidget ( )
返回类型: PySide.QtGui.QWidget

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

PySide.QtGui.QMainWindow. corner ( corner )
参数: corner PySide.QtCore.Qt.Corner
返回类型: PySide.QtCore.Qt.DockWidgetArea
PySide.QtGui.QMainWindow. createPopupMenu ( )
返回类型: PySide.QtGui.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 a null pointer.

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.

PySide.QtGui.QMainWindow. dockOptions ( )
返回类型: PySide.QtGui.QMainWindow.DockOptions

This property holds the docking behavior of PySide.QtGui.QMainWindow .

默认值为 AnimatedDocks | AllowTabbedDocks .

PySide.QtGui.QMainWindow. dockWidgetArea ( dockwidget )
参数: dockwidget PySide.QtGui.QDockWidget
返回类型: PySide.QtCore.Qt.DockWidgetArea

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

PySide.QtGui.QMainWindow. documentMode ( )
返回类型: PySide.QtCore.bool

This property holds whether the tab bar for tabbed dockwidgets is set to document mode..

默认为 false。

PySide.QtGui.QMainWindow. iconSize ( )
返回类型: PySide.QtCore.QSize

This property holds size of toolbar icons in this mainwindow..

The default is the default tool bar icon size of the GUI style. Note that the icons used must be at least of this size as the icons are only scaled down.

PySide.QtGui.QMainWindow. iconSizeChanged ( iconSize )
参数: iconSize PySide.QtCore.QSize
PySide.QtGui.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.

PySide.QtGui.QMainWindow. insertToolBarBreak ( before )
参数: before PySide.QtGui.QToolBar

Inserts a toolbar break before the toolbar specified by before .

PySide.QtGui.QMainWindow. isAnimated ( )
返回类型: PySide.QtCore.bool

This property holds whether manipulating dock widgets and tool bars is animated.

When a dock widget or tool bar is dragged over the main window, the main window adjusts its contents to indicate where the dock widget or tool bar will be docked if it is dropped. Setting this property causes PySide.QtGui.QMainWindow to move its contents in a smooth animation. Clearing this property causes the contents to snap into their new positions.

By default, this property is set. It may be cleared if the main window contains widgets which are slow at resizing or repainting themselves.

Setting this property is identical to setting the AnimatedDocks option using PySide.QtGui.QMainWindow.setDockOptions() .

PySide.QtGui.QMainWindow. isDockNestingEnabled ( )
返回类型: PySide.QtCore.bool

This property holds whether docks can be nested.

If this property is false, dock areas can only contain a single row (horizontal or vertical) of dock widgets. If this property is true, the area occupied by a dock widget can be split in either direction to contain more dock widgets.

Dock nesting is only necessary in applications that contain a lot of dock widgets. It gives the user greater freedom in organizing their main window. However, dock nesting leads to more complex (and less intuitive) behavior when a dock widget is dragged over the main window, since there are more ways in which a dropped dock widget may be placed in the dock area.

Setting this property is identical to setting the AllowNestedDocks option using PySide.QtGui.QMainWindow.setDockOptions() .

PySide.QtGui.QMainWindow. isSeparator ( pos )
参数: pos PySide.QtCore.QPoint
返回类型: PySide.QtCore.bool
PySide.QtGui.QMainWindow. menuBar ( )
返回类型: PySide.QtGui.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 PySide.QtGui.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()
												
PySide.QtGui.QMainWindow. menuWidget ( )
返回类型: PySide.QtGui.QWidget

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

PySide.QtGui.QMainWindow. removeDockWidget ( dockwidget )
参数: dockwidget PySide.QtGui.QDockWidget

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

PySide.QtGui.QMainWindow. removeToolBar ( toolbar )
参数: toolbar PySide.QtGui.QToolBar

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

PySide.QtGui.QMainWindow. removeToolBarBreak ( before )
参数: before PySide.QtGui.QToolBar

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

PySide.QtGui.QMainWindow. restoreDockWidget ( dockwidget )
参数: dockwidget PySide.QtGui.QDockWidget
返回类型: PySide.QtCore.bool

Restores the state of dockwidget if it is created after the call to PySide.QtGui.QMainWindow.restoreState() . Returns true if the state was restored; otherwise returns false.

PySide.QtGui.QMainWindow. restoreState ( state [ , version=0 ] )
参数:
返回类型:

PySide.QtCore.bool

Restores the state of this mainwindow's toolbars and dockwidgets. 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 .

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

def readSettings(self):
    settings = QSettings("MyCompany", "MyApp")
    restoreGeometry(settings.value("myWidget/geometry"))
    restoreState(settings.value("myWidget/windowState"))
												
PySide.QtGui.QMainWindow. saveState ( [ version=0 ] )
参数: version PySide.QtCore.int
返回类型: PySide.QtCore.QByteArray

Saves the current state of this mainwindow's toolbars and dockwidgets. The version number is stored as part of the data.

PySide.QtCore.QObject.objectName() property is used to identify each PySide.QtGui.QToolBar and PySide.QtGui.QDockWidget . You should make sure that this property is unique for each PySide.QtGui.QToolBar and PySide.QtGui.QDockWidget you add to the PySide.QtGui.QMainWindow

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

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

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

This property holds whether manipulating dock widgets and tool bars is animated.

When a dock widget or tool bar is dragged over the main window, the main window adjusts its contents to indicate where the dock widget or tool bar will be docked if it is dropped. Setting this property causes PySide.QtGui.QMainWindow to move its contents in a smooth animation. Clearing this property causes the contents to snap into their new positions.

By default, this property is set. It may be cleared if the main window contains widgets which are slow at resizing or repainting themselves.

Setting this property is identical to setting the AnimatedDocks option using PySide.QtGui.QMainWindow.setDockOptions() .

PySide.QtGui.QMainWindow. setCentralWidget ( widget )
参数: widget PySide.QtGui.QWidget

设置给定 widget 成为主窗口的中心小部件。

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

PySide.QtGui.QMainWindow. setCorner ( corner , area )
参数:
  • corner PySide.QtCore.Qt.Corner
  • area PySide.QtCore.Qt.DockWidgetArea
PySide.QtGui.QMainWindow. setDockNestingEnabled ( enabled )
参数: enabled PySide.QtCore.bool

This property holds whether docks can be nested.

If this property is false, dock areas can only contain a single row (horizontal or vertical) of dock widgets. If this property is true, the area occupied by a dock widget can be split in either direction to contain more dock widgets.

Dock nesting is only necessary in applications that contain a lot of dock widgets. It gives the user greater freedom in organizing their main window. However, dock nesting leads to more complex (and less intuitive) behavior when a dock widget is dragged over the main window, since there are more ways in which a dropped dock widget may be placed in the dock area.

Setting this property is identical to setting the AllowNestedDocks option using PySide.QtGui.QMainWindow.setDockOptions() .

PySide.QtGui.QMainWindow. setDockOptions ( options )
参数: options PySide.QtGui.QMainWindow.DockOptions

This property holds the docking behavior of PySide.QtGui.QMainWindow .

默认值为 AnimatedDocks | AllowTabbedDocks .

PySide.QtGui.QMainWindow. setDocumentMode ( enabled )
参数: enabled PySide.QtCore.bool

This property holds whether the tab bar for tabbed dockwidgets is set to document mode..

默认为 false。

PySide.QtGui.QMainWindow. setIconSize ( iconSize )
参数: iconSize PySide.QtCore.QSize

This property holds size of toolbar icons in this mainwindow..

The default is the default tool bar icon size of the GUI style. Note that the icons used must be at least of this size as the icons are only scaled down.

PySide.QtGui.QMainWindow. setMenuBar ( menubar )
参数: menubar PySide.QtGui.QMenuBar

把主窗口菜单栏设为 menuBar .

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

PySide.QtGui.QMainWindow. setMenuWidget ( menubar )
参数: menubar PySide.QtGui.QWidget

把主窗口菜单栏设为 menuBar .

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

PySide.QtGui.QMainWindow. setStatusBar ( statusbar )
参数: statusbar PySide.QtGui.QStatusBar

把主窗口状态栏设为 statusbar .

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

PySide.QtGui.QMainWindow. setTabPosition ( areas , tabPosition )
参数:
PySide.QtGui.QMainWindow. setTabShape ( tabShape )
参数: tabShape PySide.QtGui.QTabWidget.TabShape

This property holds the tab shape used for tabbed dock widgets..

默认为 QTabWidget.Rounded .

PySide.QtGui.QMainWindow. setToolButtonStyle ( toolButtonStyle )
参数: toolButtonStyle PySide.QtCore.Qt.ToolButtonStyle

This property holds style of toolbar buttons in this mainwindow..

默认为 Qt.ToolButtonIconOnly .

PySide.QtGui.QMainWindow. setUnifiedTitleAndToolBarOnMac ( set )
参数: set PySide.QtCore.bool

This property holds whether the window uses the unified title and toolbar look on Mac OS X.

This property is false by default and only has any effect on Mac OS X 10.4 or higher.

If set to true, then the top toolbar area is replaced with a Carbon HIToolbar or a Cocoa NSToolbar (depending on whether Qt was built with Carbon or Cocoa). All toolbars in the top toolbar area and any toolbars added afterwards are moved to that. This means a couple of things.

  • QToolBars in this toolbar area are not movable and you cannot drag other toolbars to it
  • Toolbar breaks are not respected or preserved
  • Any custom widgets in the toolbar will not be shown if the toolbar becomes too small (only actions will be shown)
  • Before Qt 4.5, if you called PySide.QtGui.QWidget.showFullScreen() on the main window, the QToolbar would disappear since it is considered to be part of the title bar. Qt 4.5 and up will now work around this by pulling the toolbars out and back into the regular toolbar and vice versa when you swap out.

Setting this back to false will remove these restrictions.

Qt.WA_MacBrushedMetal attribute takes precedence over this property.

PySide.QtGui.QMainWindow. splitDockWidget ( after , dockwidget , orientation )
参数:
PySide.QtGui.QMainWindow. statusBar ( )
返回类型: PySide.QtGui.QStatusBar

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

PySide.QtGui.QMainWindow. tabPosition ( area )
参数: area PySide.QtCore.Qt.DockWidgetArea
返回类型: PySide.QtGui.QTabWidget.TabPosition
PySide.QtGui.QMainWindow. tabShape ( )
返回类型: PySide.QtGui.QTabWidget.TabShape

This property holds the tab shape used for tabbed dock widgets..

默认为 QTabWidget.Rounded .

PySide.QtGui.QMainWindow. tabifiedDockWidgets ( dockwidget )
参数: dockwidget PySide.QtGui.QDockWidget
返回类型:

Returns the dock widgets that are tabified together with dockwidget .

PySide.QtGui.QMainWindow. tabifyDockWidget ( first , second )
参数:

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

PySide.QtGui.QMainWindow. toolBarArea ( toolbar )
参数: toolbar PySide.QtGui.QToolBar
返回类型: PySide.QtCore.Qt.ToolBarArea

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

PySide.QtGui.QMainWindow. toolBarBreak ( toolbar )
参数: toolbar PySide.QtGui.QToolBar
返回类型: PySide.QtCore.bool

Returns whether there is a toolbar break before the toolbar .

PySide.QtGui.QMainWindow. toolButtonStyle ( )
返回类型: PySide.QtCore.Qt.ToolButtonStyle

This property holds style of toolbar buttons in this mainwindow..

默认为 Qt.ToolButtonIconOnly .

PySide.QtGui.QMainWindow. toolButtonStyleChanged ( toolButtonStyle )
参数: toolButtonStyle PySide.QtCore.Qt.ToolButtonStyle
PySide.QtGui.QMainWindow. unifiedTitleAndToolBarOnMac ( )
返回类型: PySide.QtCore.bool

This property holds whether the window uses the unified title and toolbar look on Mac OS X.

This property is false by default and only has any effect on Mac OS X 10.4 or higher.

If set to true, then the top toolbar area is replaced with a Carbon HIToolbar or a Cocoa NSToolbar (depending on whether Qt was built with Carbon or Cocoa). All toolbars in the top toolbar area and any toolbars added afterwards are moved to that. This means a couple of things.

  • QToolBars in this toolbar area are not movable and you cannot drag other toolbars to it
  • Toolbar breaks are not respected or preserved
  • Any custom widgets in the toolbar will not be shown if the toolbar becomes too small (only actions will be shown)
  • Before Qt 4.5, if you called PySide.QtGui.QWidget.showFullScreen() on the main window, the QToolbar would disappear since it is considered to be part of the title bar. Qt 4.5 and up will now work around this by pulling the toolbars out and back into the regular toolbar and vice versa when you swap out.

Setting this back to false will remove these restrictions.

Qt.WA_MacBrushedMetal attribute takes precedence over this property.