The PySide.QtGui.QMainWindow class provides a main application window.
A main window provides a framework for building an application’s user interface. Qt has PySide.QtGui.QMainWindow and its related classes 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 , and a PySide.QtGui.QStatusBar . The layout has a center area that can be occupied by any kind of widget. You can see an image of the layout below.
Note
Creating a main window without a central widget is not supported. You must have a central widget even if it is just a placeholder.
A central widget will typically be a standard Qt widget such as a PySide.QtGui.QTextEdit or a PySide.QtGui.QGraphicsView . Custom widgets can also be used for advanced applications. You set the central widget with setCentralWidget() .
Main windows have either a single (SDI) or multiple (MDI) document interface. You create MDI applications in Qt by using a PySide.QtGui.QMdiArea as the central widget.
We will now examine each of the other widgets that can be added to a main window. We give examples on how to create and add them.
Toolbars are implemented in the PySide.QtGui.QToolBar class. You add a toolbar to a main window with addToolBar() .
You control the initial position of toolbars by assigning them to a specific Qt.ToolBarArea . You can split an area by inserting a toolbar break - think of this as a line break in text editing - with addToolBarBreak() or insertToolBarBreak() . You can also restrict placement by the user with QToolBar.setAllowedAreas() and QToolBar.setMovable() .
The size of toolbar icons can be retrieved with iconSize() . The sizes are platform dependent; you can set a fixed size with setIconSize() . You can alter the appearance of all tool buttons in the toolbars with setToolButtonStyle() .
An example of toolbar creation follows:
def createToolBars(self): fileToolBar = addToolBar(tr("File")) fileToolBar.addAction(Act)
Dock widgets are implemented in the PySide.QtGui.QDockWidget class. A dock widget is a window that can be docked into the main window. You add dock widgets to a main window with addDockWidget() .
There are four dock widget areas as given by the 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)
You can set a status bar with setStatusBar() , but one is created the first time statusBar() (which returns the main window’s status bar) is called. See PySide.QtGui.QStatusBar for information on how to use it.
PySide.QtGui.QMainWindow can store the state of its layout with saveState() ; it can later be retrieved with restoreState() . It is the position and size (relative to the size of the main window) of the toolbars and dock widgets that are stored.
See also
PySide.QtGui.QMenuBar PySide.QtGui.QToolBar PySide.QtGui.QStatusBar PySide.QtGui.QDockWidget Application Example Dock Widgets Example MDI Example SDI Example Menus Example
Parameters: |
|
---|
This enum contains flags that specify the docking behavior of PySide.QtGui.QMainWindow .
Constant | Description |
---|---|
QMainWindow.AnimatedDocks | Identical to the animated() property. |
QMainWindow.AllowNestedDocks | Identical to the dockNestingEnabled() property. |
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 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 AllowTabbedDocks . See also 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.
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: |
|
---|
Parameters: | title – unicode |
---|---|
Return type: | PySide.QtGui.QToolBar |
This is an overloaded function.
Creates a PySide.QtGui.QToolBar object, setting its window title to title , and inserts it into the top toolbar area.
Parameters: | toolbar – PySide.QtGui.QToolBar |
---|
This is an overloaded function.
Equivalent of calling addToolBar( Qt.TopToolBarArea , toolbar )
Parameters: | area – PySide.QtCore.Qt.ToolBarArea |
---|
Return type: | PySide.QtGui.QWidget |
---|
Returns the central widget for the main window. This function returns zero if the central widget has not been set.
Parameters: | corner – PySide.QtCore.Qt.Corner |
---|---|
Return type: | PySide.QtCore.Qt.DockWidgetArea |
Return type: | 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.
Return type: | PySide.QtGui.QMainWindow.DockOptions |
---|
This property holds the docking behavior of PySide.QtGui.QMainWindow .
The default value is AnimatedDocks | AllowTabbedDocks .
Parameters: | dockwidget – PySide.QtGui.QDockWidget |
---|---|
Return type: | PySide.QtCore.Qt.DockWidgetArea |
Returns the Qt.DockWidgetArea for dockwidget . If dockwidget has not been added to the main window, this function returns Qt::NoDockWidgetArea .
See also
PySide.QtGui.QMainWindow.addDockWidget() PySide.QtGui.QMainWindow.splitDockWidget() Qt.DockWidgetArea
Return type: | PySide.QtCore.bool |
---|
This property holds whether the tab bar for tabbed dockwidgets is set to document mode..
The default is false.
See also
Return type: | 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.
Parameters: | iconSize – PySide.QtCore.QSize |
---|
Parameters: |
|
---|
Inserts the 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.
Parameters: | before – PySide.QtGui.QToolBar |
---|
Inserts a toolbar break before the toolbar specified by before .
Return type: | 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() .
Return type: | 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() .
Parameters: | pos – PySide.QtCore.QPoint |
---|---|
Return type: | PySide.QtCore.bool |
Return type: | 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()
Return type: | PySide.QtGui.QWidget |
---|
Returns the menu bar for the main window. This function returns null if a menu bar hasn’t been constructed yet.
Parameters: | dockwidget – PySide.QtGui.QDockWidget |
---|
Removes the dockwidget from the main window layout and hides it. Note that the dockwidget is not deleted.
Parameters: | toolbar – PySide.QtGui.QToolBar |
---|
Removes the toolbar from the main window layout and hides it. Note that the toolbar is not deleted.
Parameters: | before – PySide.QtGui.QToolBar |
---|
Removes a toolbar break previously inserted before the toolbar specified by before .
Parameters: | dockwidget – PySide.QtGui.QDockWidget |
---|---|
Return type: | 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.
Parameters: |
|
---|---|
Return type: | 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 .
To restore geometry saved using PySide.QtCore.QSettings , you can use code like this:
def readSettings(self):
settings = QSettings("MyCompany", "MyApp")
restoreGeometry(settings.value("myWidget/geometry"))
restoreState(settings.value("myWidget/windowState"))
Parameters: | version – PySide.QtCore.int |
---|---|
Return type: | PySide.QtCore.QByteArray |
Saves the current state of this mainwindow’s toolbars and dockwidgets. The version number is stored as part of the data.
The 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() .
To save the geometry when the window closes, you can implement a close event like this:
def closeEvent(self, event):
settings = QSettings("MyCompany", "MyApp")
settings.setValue("geometry", self.saveGeometry())
settings.setValue("windowState", self.saveState())
QMainWindow.closeEvent(self, event)
Parameters: | 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() .
Parameters: | widget – PySide.QtGui.QWidget |
---|
Sets the given widget to be the main window’s central widget.
Note: PySide.QtGui.QMainWindow takes ownership of the widget pointer and deletes it at the appropriate time.
Parameters: |
|
---|
Parameters: | 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() .
Parameters: | options – PySide.QtGui.QMainWindow.DockOptions |
---|
This property holds the docking behavior of PySide.QtGui.QMainWindow .
The default value is AnimatedDocks | AllowTabbedDocks .
Parameters: | enabled – PySide.QtCore.bool |
---|
This property holds whether the tab bar for tabbed dockwidgets is set to document mode..
The default is false.
See also
Parameters: | 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.
Parameters: | menubar – PySide.QtGui.QMenuBar |
---|
Sets the menu bar for the main window to menuBar .
Note: PySide.QtGui.QMainWindow takes ownership of the menuBar pointer and deletes it at the appropriate time.
See also
Parameters: | menubar – PySide.QtGui.QWidget |
---|
Sets the menu bar for the main window to menuBar .
PySide.QtGui.QMainWindow takes ownership of the menuBar pointer and deletes it at the appropriate time.
Parameters: | statusbar – PySide.QtGui.QStatusBar |
---|
Sets the status bar for the main window to statusbar .
Setting the status bar to 0 will remove it from the main window. Note that PySide.QtGui.QMainWindow takes ownership of the statusbar pointer and deletes it at the appropriate time.
See also
Parameters: |
|
---|
Parameters: | tabShape – PySide.QtGui.QTabWidget.TabShape |
---|
This property holds the tab shape used for tabbed dock widgets..
The default is QTabWidget.Rounded .
Parameters: | toolButtonStyle – PySide.QtCore.Qt.ToolButtonStyle |
---|
This property holds style of toolbar buttons in this mainwindow..
The default is Qt.ToolButtonIconOnly .
Parameters: | 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.
Setting this back to false will remove these restrictions.
The Qt.WA_MacBrushedMetal attribute takes precedence over this property.
Parameters: |
|
---|
Return type: | PySide.QtGui.QStatusBar |
---|
Returns the status bar for the main window. This function creates and returns an empty status bar if the status bar does not exist.
Parameters: | area – PySide.QtCore.Qt.DockWidgetArea |
---|---|
Return type: | PySide.QtGui.QTabWidget.TabPosition |
Return type: | PySide.QtGui.QTabWidget.TabShape |
---|
This property holds the tab shape used for tabbed dock widgets..
The default is QTabWidget.Rounded .
Parameters: | dockwidget – PySide.QtGui.QDockWidget |
---|---|
Return type: |
Returns the dock widgets that are tabified together with dockwidget .
Parameters: |
|
---|
Moves second dock widget on top of first dock widget, creating a tabbed docked area in the main window.
Parameters: | toolbar – PySide.QtGui.QToolBar |
---|---|
Return type: | PySide.QtCore.Qt.ToolBarArea |
Returns the Qt.ToolBarArea for toolbar . If toolbar has not been added to the main window, this function returns Qt::NoToolBarArea .
See also
PySide.QtGui.QMainWindow.addToolBar() PySide.QtGui.QMainWindow.addToolBarBreak() Qt.ToolBarArea
Parameters: | toolbar – PySide.QtGui.QToolBar |
---|---|
Return type: | PySide.QtCore.bool |
Returns whether there is a toolbar break before the toolbar .
Return type: | PySide.QtCore.Qt.ToolButtonStyle |
---|
This property holds style of toolbar buttons in this mainwindow..
The default is Qt.ToolButtonIconOnly .
Parameters: | toolButtonStyle – PySide.QtCore.Qt.ToolButtonStyle |
---|
Return type: | 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.
Setting this back to false will remove these restrictions.
The Qt.WA_MacBrushedMetal attribute takes precedence over this property.