QMainWindowclass provides a main application window. 更多 …
def
addDockWidget
(area, dockwidget)
def
addDockWidget
(area, dockwidget, orientation)
def
addToolBar
(area, toolbar)
def
addToolBar
(title)
def
addToolBar
(toolbar)
def
addToolBarBreak
([area=Qt.TopToolBarArea])
def
centralWidget
()
def
corner
(corner)
def
dockOptions
()
def
dockWidgetArea
(dockwidget)
def
documentMode
()
def
iconSize
()
def
insertToolBar
(before, toolbar)
def
insertToolBarBreak
(before)
def
isAnimated
()
def
isDockNestingEnabled
()
def
isSeparator
(pos)
def
menuBar
()
def
menuWidget
()
def
removeDockWidget
(dockwidget)
def
removeToolBar
(toolbar)
def
removeToolBarBreak
(before)
def
resizeDocks
(docks, sizes, orientation)
def
restoreDockWidget
(dockwidget)
def
restoreState
(state[, version=0])
def
saveState
([version=0])
def
setCentralWidget
(widget)
def
setCorner
(corner, area)
def
setDockOptions
(options)
def
setDocumentMode
(enabled)
def
setIconSize
(iconSize)
def
setMenuBar
(menubar)
def
setMenuWidget
(menubar)
def
setStatusBar
(statusbar)
def
setTabPosition
(areas, tabPosition)
def
setTabShape
(tabShape)
def
setToolButtonStyle
(toolButtonStyle)
def
splitDockWidget
(after, dockwidget, orientation)
def
statusBar
()
def
tabPosition
(area)
def
tabShape
()
def
tabifiedDockWidgets
(dockwidget)
def
tabifyDockWidget
(first, second)
def
takeCentralWidget
()
def
toolBarArea
(toolbar)
def
toolBarBreak
(toolbar)
def
toolButtonStyle
()
def
unifiedTitleAndToolBarOnMac
()
def
createPopupMenu
()
def
setAnimated
(enabled)
def
setDockNestingEnabled
(enabled)
def
setUnifiedTitleAndToolBarOnMac
(set)
def
iconSizeChanged
(iconSize)
def
tabifiedDockWidgetActivated
(dockWidget)
def
toolButtonStyleChanged
(toolButtonStyle)
A main window provides a framework for building an application’s user interface. Qt has
QMainWindowand its 相关类 for main window management.QMainWindowhas its own layout to which you can addQToolBars,QDockWidgets, aQMenuBar,和QStatusBar。布局具有可以被任何种类 Widget 占据的中心区域。见以下布局图像。![]()
注意
不支持创建没有中心 Widget 的主窗口。必须有中心小部件,即使它只是占位符。
中心小部件通常是标准 Qt Widget,譬如
QTextEdit或QGraphicsView。自定义 Widget 还可以用于高级应用程序。设置中心 Widget 采用setCentralWidget().主窗口有 SDI (单文档界面) 或 MDI (多文档界面)。在 Qt 中创建 MDI 应用程序通过使用
QMdiArea作为中心 Widget。现在,我们将研究可以被添加到主窗口的每一其它 Widget。举例说明如何创建和添加它们。
工具栏的实现在
QToolBar类。把工具栏添加到主窗口采用addToolBar().控制工具栏的初始位置通过把它们赋值给特定
ToolBarArea。可以通过插入工具栏打断分割区域 - 把它想像成文本编辑中的换行符 - 采用addToolBarBreak()orinsertToolBarBreak()。还可以限定用户放置采用setAllowedAreas()andsetMovable().可以检索工具栏图标的尺寸采用
iconSize()。尺寸从属平台;可以设置固定尺寸采用setIconSize()。可以更改工具栏中所有工具按钮的外观采用setToolButtonStyle().以下是创建工具栏的范例:
void MainWindow::createToolBars() { fileToolBar = addToolBar(tr("File")); fileToolBar->addAction(newAct);
停放 Widget 的实现在
QDockWidget类。停放 Widget 是可以停放在主窗口中的窗口。可以把停放 Widget 添加到主窗口采用addDockWidget().有 4 个停放 Widget 区域,给出通过
DockWidgetAreaenum: left, right, top, and bottom. You can specify which dock widget area that should occupy the corners where the areas overlap withsetCorner(). By default each area can only contain one row (vertical or horizontal) of dock widgets, but if you enable nesting withsetDockNestingEnabled(), dock widgets can be added in either direction.Two dock widgets may also be stacked on top of each other. A
QTabBaris 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. SeeQStatusBar了解如何使用它的有关信息。
QMainWindowcan store the state of its layout withsaveState();稍后,可以检索它采用restoreState()。它是存储的工具栏和停放 Widget 的位置和尺寸 (相对于主窗口的尺寸)。另请参阅
QMenuBarQToolBarQStatusBarQDockWidget应用程序范例 停放 Widget 范例 MDI 范例 SDI 范例 菜单范例
QMainWindow
(
[
parent=None
[
,
flags=Qt.WindowFlags()
]
]
)
¶
- param parent
- 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 |
等同于
|
|
QMainWindow.AllowNestedDocks |
等同于
|
|
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
|
|
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
)
¶
area
–
DockWidgetArea
dockwidget
–
QDockWidget
添加给定
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
)
¶
area
–
ToolBarArea
toolbar
–
QToolBar
添加
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.
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
(
)
¶
返回主窗口的中心 Widget。此函数返回零,若中心 Widget 未设置。
另请参阅
PySide2.QtWidgets.QMainWindow.
corner
(
corner
)
¶
corner
–
Corner
DockWidgetArea
Returns the dock widget area that occupies the specified
corner
.
另请参阅
PySide2.QtWidgets.QMainWindow.
createPopupMenu
(
)
¶
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
另请参阅
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
另请参阅
PySide2.QtWidgets.QMainWindow.
iconSize
(
)
¶
QSize
另请参阅
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
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()
另请参阅
Returns the menu bar for the main window. This function returns null if a menu bar hasn’t been constructed yet.
另请参阅
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
另请参阅
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.
另请参阅
PySide2.QtWidgets.QMainWindow.
setCorner
(
corner
,
area
)
¶
corner
–
Corner
area
–
DockWidgetArea
Sets the given dock widget
area
to occupy the specified
corner
.
另请参阅
PySide2.QtWidgets.QMainWindow.
setDockNestingEnabled
(
enabled
)
¶
enabled
–
bool
PySide2.QtWidgets.QMainWindow.
setDockOptions
(
options
)
¶
options
–
DockOptions
另请参阅
PySide2.QtWidgets.QMainWindow.
setDocumentMode
(
enabled
)
¶
enabled
–
bool
另请参阅
PySide2.QtWidgets.QMainWindow.
setIconSize
(
iconSize
)
¶
iconSize
–
QSize
另请参阅
PySide2.QtWidgets.QMainWindow.
setMenuBar
(
menubar
)
¶
menubar
–
QMenuBar
把主窗口菜单栏设为
menuBar
.
注意:
QMainWindow
拥有所有权对于
menuBar
pointer and deletes it at the appropriate time.
另请参阅
PySide2.QtWidgets.QMainWindow.
setMenuWidget
(
menubar
)
¶
menubar
–
QWidget
把主窗口菜单栏设为
menuBar
.
QMainWindow
拥有所有权对于
menuBar
pointer and deletes it at the appropriate time.
另请参阅
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.
另请参阅
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
另请参阅
PySide2.QtWidgets.QMainWindow.
setToolButtonStyle
(
toolButtonStyle
)
¶
toolButtonStyle
–
ToolButtonStyle
另请参阅
PySide2.QtWidgets.QMainWindow.
setUnifiedTitleAndToolBarOnMac
(
set
)
¶
set
–
bool
PySide2.QtWidgets.QMainWindow.
splitDockWidget
(
after
,
dockwidget
,
orientation
)
¶
after
–
QDockWidget
dockwidget
–
QDockWidget
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
(
)
¶
返回用于主窗口的状态栏。此函数创建并返回空状态栏,若状态栏不存在。
另请参阅
PySide2.QtWidgets.QMainWindow.
tabPosition
(
area
)
¶
area
–
DockWidgetArea
TabPosition
返回选项卡位置为
area
.
注意
VerticalTabs
停放选项覆盖由此函数返回的选项卡位置。
PySide2.QtWidgets.QMainWindow.
tabShape
(
)
¶
TabShape
另请参阅
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
)
¶
first
–
QDockWidget
second
–
QDockWidget
移动
second
dock widget on top of
first
dock widget, creating a tabbed docked area in the main window.
PySide2.QtWidgets.QMainWindow.
takeCentralWidget
(
)
¶
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