pyqt5-QMainWindow
1、介绍
QMainWindow是一个窗口组件,也是最常用的主窗口。
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.uic import loadUi
class my_window:
def __init__(self):
self.ui = loadUi('ui\\main_window.ui')
self.ui: QMainWindow
self.ui.show()
if __name__ == "__main__":
app = QApplication([])
w = my_window()
sys.exit(app.exec_())
- self.ui这里即QMainWindow对象
- 某些文章中提到不支持创建没有中央小部件的主窗口,但实际测试可以
- QMainWindow和QWidget的主要区别是,前者具有菜单栏和工具栏设置
2、类和初始化
class QMainWindow(QWidget):
def __init__(self, parent=None, flags, Qt_WindowFlags=None, Qt_WindowType=None, *args, **kwargs):
pass
3、属性
类中具有的一些全局属性:
AllowNestedDocks = 2
AllowTabbedDocks = 4
AnimatedDocks = 1
ForceTabbedDocks = 8
GroupedDragging = 32
VerticalTabs = 16
4、方法
大多数方法在QWidget中已经声明,除了init和事件之外,几乎所有的方法如下:
QMainWindow(parent: typing.Optional[QWidget] = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags())
addDockWidget(self, area: Qt.DockWidgetArea, dockwidget: QDockWidget)
addDockWidget(self, area: Qt.DockWidgetArea, dockwidget: QDockWidget, orientation: Qt.Orientation)
addToolBar(self, area: Qt.ToolBarArea, toolbar: QToolBar)
addToolBar(self, toolbar: QToolBar)
addToolBar(self, title: str) -> QToolBar
addToolBarBreak(self, area: Qt.ToolBarArea = Qt.TopToolBarArea)
centralWidget(self) -> QWidget
corner(self, corner: Qt.Corner) -> Qt.DockWidgetArea
createPopupMenu(self) -> QMenu
dockOptions(self) -> QMainWindow.DockOptions
dockWidgetArea(self, dockwidget: QDockWidget) -> Qt.DockWidgetArea
documentMode(self) -> bool
iconSize(self) -> QSize
insertToolBar(self, before: QToolBar, toolbar: QToolBar)
insertToolBarBreak(self, before: QToolBar)
isAnimated(self) -> bool
isDockNestingEnabled(self) -> bool
isSeparator(self, pos: QPoint) -> bool
menuBar(self) -> QMenuBar
menuWidget(self) -> QWidget
removeDockWidget(self, dockwidget: QDockWidget)
removeToolBar(self, toolbar: QToolBar)
removeToolBarBreak(self, before: QToolBar)
resizeDocks(self, docks: Iterable[QDockWidget], sizes: Iterable[int], orientation: Qt.Orientation)
restoreDockWidget(self, dockwidget: QDockWidget) -> bool
restoreState(self, state: Union[QByteArray, bytes, bytearray], version: int = 0) -> bool
saveState(self, version: int = 0) -> QByteArray
setAnimated(self, enabled: bool)
setCentralWidget(self, widget: QWidget)
setCorner(self, corner: Qt.Corner, area: Qt.DockWidgetArea)
setDockNestingEnabled(self, enabled: bool)
setDockOptions(self, options: Union[QMainWindow.DockOptions, QMainWindow.DockOption])
setDocumentMode(self, enabled: bool)
setIconSize(self, iconSize: QSize)
setMenuBar(self, menubar: QMenuBar)
setMenuWidget(self, menubar: QWidget)
setStatusBar(self, statusbar: QStatusBar)
setTabPosition(self, areas: Union[Qt.DockWidgetAreas, Qt.DockWidgetArea], tabPosition: QTabWidget.TabPosition)
setTabShape(self, tabShape: QTabWidget.TabShape)
setToolButtonStyle(self, toolButtonStyle: Qt.ToolButtonStyle)
setUnifiedTitleAndToolBarOnMac(self, set: bool)
splitDockWidget(self, after: QDockWidget, dockwidget: QDockWidget, orientation: Qt.Orientation)
statusBar(self) -> QStatusBar
tabifiedDockWidgets(self, dockwidget: QDockWidget) -> List[QDockWidget]
tabifyDockWidget(self, first: QDockWidget, second: QDockWidget)
tabPosition(self, area: Qt.DockWidgetArea) -> QTabWidget.TabPosition
tabShape(self) -> QTabWidget.TabShape
takeCentralWidget(self) -> QWidget
toolBarArea(self, toolbar: QToolBar) -> Qt.ToolBarArea
toolBarBreak(self, toolbar: QToolBar) -> bool
toolButtonStyle(self) -> Qt.ToolButtonStyle
unifiedTitleAndToolBarOnMac(self) -> bool
---------------
contextMenuEvent(self, event: QContextMenuEvent)
event(self, event: QEvent) -> bool
---------------
iconSizeChanged(self, iconSize: QSize) [signal]
tabifiedDockWidgetActivated(self, dockWidget: QDockWidget) [signal]
toolButtonStyleChanged(self, toolButtonStyle: Qt.ToolButtonStyle) [signal]
(1)menuBar
menuBar(self) -> QMenuBar
- 返回菜单栏对象
(2)
restoreDockWidget(self, dockwidget: QDockWidget) -> bool
(3)setIconSize
setIconSize(self, iconSize: QSize)
设置图标大小
(1)parent
- 获取父组件,注意不是当前组件继承的父类,而是该组件所绑定到的组件,一般是指在开发过程中声明组件时parent属性初始化指向的组件对象
- 测试,QMainWindow对象这里默认没有父组件,返回None值
(2)objectName
- 获取对象的名称,返回类型str,这是组件对象所必须的属性,在同一窗口下,组件名称需要唯一
- 组件名称可以用于调用对象
(2)setObjectName
- 设置组件对象的名称,参数为str类型
(3)iconSize
- 返回窗口图标的大小,类型PyQt5.QtCore.QSize(30, 30)
(4)setIconSize
setIconSize(self, QSize)
- 设置图标的大小
5、事件
QMainWindow特地声明的事件只有这三个:
iconSizeChanged(self, iconSize: QSize) [signal]
tabifiedDockWidgetActivated(self, dockWidget: QDockWidget) [signal]
toolButtonStyleChanged(self, toolButtonStyle: Qt.ToolButtonStyle) [signal]