pyqt5-QWidget
1、介绍
QWidget是几乎所有pyqt显示用组件的直接或间接父类,其声明的属性和方法很具有代表性。
QWidget既可以是窗口组件,独立显示,可以容纳其他组件。也可以是一般组件,被其他组件容纳。
常用的窗口组件包括QWidget、QMainWindow和QDialog。其它组件继承QWidget,也能够作为窗口独立存在,但是一般不会使用。
- QWidget作为最简单的组件,被广泛使用
- QMainWindow相比于其他窗口组件,可以设置窗口的菜单栏
- QDialog用于对话框
2、类和初始化
class QWidget(__PyQt5_QtCore.QObject, __PyQt5_QtGui.QPaintDevice):
def __init__(self, parent=None, flags, Qt_WindowFlags=None, Qt_WindowType=None, *args, **kwargs):
pass
快速查询:
QWidget(parent: typing.Optional[QWidget] = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags())
acceptDrops(self) -> bool
accessibleDescription(self) -> str
accessibleName(self) -> str
actions(self) -> List[QAction]
activateWindow(self)
addAction(self, action: QAction)
addActions(self, actions: Iterable[QAction])
adjustSize(self)
autoFillBackground(self) -> bool
backgroundRole(self) -> QPalette.ColorRole
baseSize(self) -> QSize
childAt(self, p: QPoint) -> QWidget
childAt(self, ax: int, ay: int) -> QWidget
childrenRect(self) -> QRect
childrenRegion(self) -> QRegion
clearFocus(self)
clearMask(self)
close(self) -> bool
contentsMargins(self) -> QMargins
contentsRect(self) -> QRect
contextMenuPolicy(self) -> Qt.ContextMenuPolicy
create(self, window: PyQt5.sip.voidptr = 0, initializeWindow: bool = True, destroyOldWindow: bool = True)
createWindowContainer(window: QWindow, parent: typing.Optional[QWidget] = None, flags: Union[Qt.WindowFlags, Qt.WindowType] = 0) -> QWidget
cursor(self) -> QCursor
destroy(self, destroyWindow: bool = True, destroySubWindows: bool = True)
devType(self) -> int
effectiveWinId(self) -> PyQt5.sip.voidptr
ensurePolished(self)
find(a0: PyQt5.sip.voidptr) -> QWidget
focusNextChild(self) -> bool
focusNextPrevChild(self, next: bool) -> bool
focusPolicy(self) -> Qt.FocusPolicy
focusPreviousChild(self) -> bool
focusProxy(self) -> QWidget
focusWidget(self) -> QWidget
font(self) -> QFont
fontInfo(self) -> QFontInfo
fontMetrics(self) -> QFontMetrics
foregroundRole(self) -> QPalette.ColorRole
frameGeometry(self) -> QRect
frameSize(self) -> QSize
geometry(self) -> QRect
getContentsMargins(self) -> Tuple[int, int, int, int]
grab(self, rectangle: QRect = QRect(QPoint(0,0),QSize(-1,-1))) -> QPixmap
grabGesture(self, type: Qt.GestureType, flags: Union[Qt.GestureFlags, Qt.GestureFlag] = Qt.GestureFlags())
grabKeyboard(self)
grabMouse(self)
grabMouse(self, a0: Union[QCursor, Qt.CursorShape])
grabShortcut(self, key: Union[QKeySequence, QKeySequence.StandardKey, str, int], context: Qt.ShortcutContext = Qt.WindowShortcut) -> int
graphicsEffect(self) -> QGraphicsEffect
graphicsProxyWidget(self) -> QGraphicsProxyWidget
hasFocus(self) -> bool
hasHeightForWidth(self) -> bool
hasMouseTracking(self) -> bool
hasTabletTracking(self) -> bool
height(self) -> int
heightForWidth(self, a0: int) -> int
hide(self)
initPainter(self, painter: QPainter)
inputMethodHints(self) -> Qt.InputMethodHints
inputMethodQuery(self, a0: Qt.InputMethodQuery) -> Any
insertAction(self, before: QAction, action: QAction)
insertActions(self, before: QAction, actions: Iterable[QAction])
isActiveWindow(self) -> bool
isAncestorOf(self, child: QWidget) -> bool
isEnabled(self) -> bool
isEnabledTo(self, a0: QWidget) -> bool
isFullScreen(self) -> bool
isHidden(self) -> bool
isLeftToRight(self) -> bool
isMaximized(self) -> bool
isMinimized(self) -> bool
isModal(self) -> bool
isRightToLeft(self) -> bool
isVisible(self) -> bool
isVisibleTo(self, a0: QWidget) -> bool
isWindow(self) -> bool
isWindowModified(self) -> bool
keyboardGrabber() -> QWidget
layout(self) -> QLayout
layoutDirection(self) -> Qt.LayoutDirection
locale(self) -> QLocale
lower(self)
mapFrom(self, a0: QWidget, a1: QPoint) -> QPoint
mapFromGlobal(self, a0: QPoint) -> QPoint
mapFromParent(self, a0: QPoint) -> QPoint
mapTo(self, a0: QWidget, a1: QPoint) -> QPoint
mapToGlobal(self, a0: QPoint) -> QPoint
mapToParent(self, a0: QPoint) -> QPoint
mask(self) -> QRegion
maximumHeight(self) -> int
maximumSize(self) -> QSize
maximumWidth(self) -> int
metric(self, a0: QPaintDevice.PaintDeviceMetric) -> int
minimumHeight(self) -> int
minimumSize(self) -> QSize
minimumSizeHint(self) -> QSize
minimumWidth(self) -> int
mouseGrabber() -> QWidget
move(self, a0: QPoint)
move(self, ax: int, ay: int)
nativeParentWidget(self) -> QWidget
nextInFocusChain(self) -> QWidget
normalGeometry(self) -> QRect
overrideWindowFlags(self, type: Union[Qt.WindowFlags, Qt.WindowType])
overrideWindowState(self, state: Union[Qt.WindowStates, Qt.WindowState])
paintEngine(self) -> QPaintEngine
palette(self) -> QPalette
parentWidget(self) -> QWidget
pos(self) -> QPoint
previousInFocusChain(self) -> QWidget
raise_(self)
rect(self) -> QRect
releaseKeyboard(self)
releaseMouse(self)
releaseShortcut(self, id: int)
removeAction(self, action: QAction)
render(self, target: QPaintDevice, targetOffset: QPoint = QPoint(), sourceRegion: QRegion = QRegion(), flags: Union[QWidget.RenderFlags, QWidget.RenderFlag] = QWidget.RenderFlags(QWidget.RenderFlag.DrawWindowBackground|QWidget.RenderFlag.DrawChildren))
render(self, painter: QPainter, targetOffset: QPoint = QPoint(), sourceRegion: QRegion = QRegion(), flags: Union[QWidget.RenderFlags, QWidget.RenderFlag] = QWidget.RenderFlags(QWidget.RenderFlag.DrawWindowBackground|QWidget.RenderFlag.DrawChildren))
repaint(self)
repaint(self, x: int, y: int, w: int, h: int)
repaint(self, a0: QRect)
repaint(self, a0: QRegion)
resize(self, a0: QSize)
resize(self, w: int, h: int)
restoreGeometry(self, geometry: Union[QByteArray, bytes, bytearray]) -> bool
saveGeometry(self) -> QByteArray
screen(self) -> QScreen
scroll(self, dx: int, dy: int)
scroll(self, dx: int, dy: int, a2: QRect)
setAcceptDrops(self, on: bool)
setAccessibleDescription(self, description: str)
setAccessibleName(self, name: str)
setAttribute(self, attribute: Qt.WidgetAttribute, on: bool = True)
setAutoFillBackground(self, enabled: bool)
setBackgroundRole(self, a0: QPalette.ColorRole)
setBaseSize(self, basew: int, baseh: int)
setBaseSize(self, s: QSize)
setContentsMargins(self, left: int, top: int, right: int, bottom: int)
setContentsMargins(self, margins: QMargins)
setContextMenuPolicy(self, policy: Qt.ContextMenuPolicy)
setCursor(self, a0: Union[QCursor, Qt.CursorShape])
setDisabled(self, a0: bool)
setEnabled(self, a0: bool)
setFixedHeight(self, h: int)
setFixedSize(self, a0: QSize)
setFixedSize(self, w: int, h: int)
setFixedWidth(self, w: int)
setFocus(self)
setFocus(self, reason: Qt.FocusReason)
setFocusPolicy(self, policy: Qt.FocusPolicy)
setFocusProxy(self, a0: QWidget)
setFont(self, a0: QFont)
setForegroundRole(self, a0: QPalette.ColorRole)
setGeometry(self, a0: QRect)
setGeometry(self, ax: int, ay: int, aw: int, ah: int)
setGraphicsEffect(self, effect: QGraphicsEffect)
setHidden(self, hidden: bool)
setInputMethodHints(self, hints: Union[Qt.InputMethodHints, Qt.InputMethodHint])
setLayout(self, a0: QLayout)
setLayoutDirection(self, direction: Qt.LayoutDirection)
setLocale(self, locale: QLocale)
setMask(self, a0: QBitmap)
setMask(self, a0: QRegion)
setMaximumHeight(self, maxh: int)
setMaximumSize(self, maxw: int, maxh: int)
setMaximumSize(self, s: QSize)
setMaximumWidth(self, maxw: int)
setMinimumHeight(self, minh: int)
setMinimumSize(self, minw: int, minh: int)
setMinimumSize(self, s: QSize)
setMinimumWidth(self, minw: int)
setMouseTracking(self, enable: bool)
setPalette(self, a0: QPalette)
setParent(self, parent: QWidget)
setParent(self, parent: QWidget, f: Union[Qt.WindowFlags, Qt.WindowType])
setShortcutAutoRepeat(self, id: int, enabled: bool = True)
setShortcutEnabled(self, id: int, enabled: bool = True)
setSizeIncrement(self, w: int, h: int)
setSizeIncrement(self, s: QSize)
setSizePolicy(self, a0: QSizePolicy)
setSizePolicy(self, hor: QSizePolicy.Policy, ver: QSizePolicy.Policy)
setStatusTip(self, a0: str)
setStyle(self, a0: QStyle)
setStyleSheet(self, styleSheet: str)
setTabletTracking(self, enable: bool)
setTabOrder(a0: QWidget, a1: QWidget)
setToolTip(self, a0: str)
setToolTipDuration(self, msec: int)
setUpdatesEnabled(self, enable: bool)
setVisible(self, visible: bool)
setWhatsThis(self, a0: str)
setWindowFilePath(self, filePath: str)
setWindowFlag(self, a0: Qt.WindowType, on: bool = True)
setWindowFlags(self, type: Union[Qt.WindowFlags, Qt.WindowType])
setWindowIcon(self, icon: QIcon)
setWindowIconText(self, a0: str)
setWindowModality(self, windowModality: Qt.WindowModality)
setWindowModified(self, a0: bool)
setWindowOpacity(self, level: float)
setWindowRole(self, a0: str)
setWindowState(self, state: Union[Qt.WindowStates, Qt.WindowState])
setWindowTitle(self, a0: str)
sharedPainter(self) -> QPainter
show(self)
showFullScreen(self)
showMaximized(self)
showMinimized(self)
showNormal(self)
size(self) -> QSize
sizeHint(self) -> QSize
sizeIncrement(self) -> QSize
sizePolicy(self) -> QSizePolicy
stackUnder(self, a0: QWidget)
statusTip(self) -> str
style(self) -> QStyle
styleSheet(self) -> str
testAttribute(self, attribute: Qt.WidgetAttribute) -> bool
toolTip(self) -> str
toolTipDuration(self) -> int
underMouse(self) -> bool
ungrabGesture(self, type: Qt.GestureType)
unsetCursor(self)
unsetLayoutDirection(self)
unsetLocale(self)
update(self)
update(self, a0: QRect)
update(self, a0: QRegion)
update(self, ax: int, ay: int, aw: int, ah: int)
updateGeometry(self)
updateMicroFocus(self)
updatesEnabled(self) -> bool
visibleRegion(self) -> QRegion
whatsThis(self) -> str
width(self) -> int
window(self) -> QWidget
windowFilePath(self) -> str
windowFlags(self) -> Qt.WindowFlags
windowHandle(self) -> QWindow
windowIcon(self) -> QIcon
windowIconText(self) -> str
windowModality(self) -> Qt.WindowModality
windowOpacity(self) -> float
windowRole(self) -> str
windowState(self) -> Qt.WindowStates
windowTitle(self) -> str
windowType(self) -> Qt.WindowType
winId(self) -> PyQt5.sip.voidptr
x(self) -> int
y(self) -> int
---------------
actionEvent(self, a0: QActionEvent)
changeEvent(self, a0: QEvent)
closeEvent(self, a0: QCloseEvent)
contextMenuEvent(self, a0: QContextMenuEvent)
dragEnterEvent(self, a0: QDragEnterEvent)
dragLeaveEvent(self, a0: QDragLeaveEvent)
dragMoveEvent(self, a0: QDragMoveEvent)
dropEvent(self, a0: QDropEvent)
enterEvent(self, a0: QEvent)
event(self, a0: QEvent) -> bool
focusInEvent(self, a0: QFocusEvent)
focusOutEvent(self, a0: QFocusEvent)
hideEvent(self, a0: QHideEvent)
inputMethodEvent(self, a0: QInputMethodEvent)
keyPressEvent(self, a0: QKeyEvent)
keyReleaseEvent(self, a0: QKeyEvent)
leaveEvent(self, a0: QEvent)
mouseDoubleClickEvent(self, a0: QMouseEvent)
mouseMoveEvent(self, a0: QMouseEvent)
mousePressEvent(self, a0: QMouseEvent)
mouseReleaseEvent(self, a0: QMouseEvent)
moveEvent(self, a0: QMoveEvent)
nativeEvent(self, eventType: Union[QByteArray, bytes, bytearray], message: PyQt5.sip.voidptr) -> Tuple[bool, int]
paintEvent(self, a0: QPaintEvent)
resizeEvent(self, a0: QResizeEvent)
showEvent(self, a0: QShowEvent)
tabletEvent(self, a0: QTabletEvent)
wheelEvent(self, a0: QWheelEvent)
---------------
customContextMenuRequested(self, pos: QPoint) [signal]
windowIconChanged(self, icon: QIcon) [signal]
windowIconTextChanged(self, iconText: str) [signal]
windowTitleChanged(self, title: str) [signal]
3、属性
类的全局属性:
DrawChildren = 2
DrawWindowBackground = 1
IgnoreMask = 4
4、一般方法
4.1 位置
(1)x和y
- 分别返回当前组件相对于所在容器左上顶点在水平方向和垂直方向上的距离,类型为int,单位是像素
- 当前组件在父容器中完全显示,则x和y为正数,否则超出范围,会出现负数。
- 如果当前组件作为窗口组件独立显示,其父容器则是整个屏幕,此时是标题栏的左上顶点对应屏幕左上顶点计算
- 此时,如果当前组件向左拖动,部分超出屏幕,其x为负值
- 如果当前组件是非窗口组件,在其它容器组件中,比如在QWidget、QGroupBox等中,那么此时是当前组件的左上顶点与其父容器的左上顶点计算
- 如果父容器是窗口组件,是包含标题栏部分的,而是对应主界面区域的左上顶点
(2)pos
pos(self) -> QPoint
- 返回QPoint对象,包含x和y
- 不存在对应的设置x和y,也不存在对应设置pos的方法。一般用move或setGeometry方法实现操作
(3)move
move(self, QPoint)
move(self, int, int)
- 移动组件的位置
- 两种调用方法,设置是相对于父组件的绝对位置
4.2 尺寸
(1)width和height
- 返回组件的宽度和高度,类型为int,单位是像素
(2)size
- 返回QSize对象,包含width和height数据
(3)setBaseSize
setBaseSize(self, basew: int, baseh: int)
setBaseSize(self, s: QSize)
- 设置初始的尺寸
(4)resize
resize(self, a0: QSize)
resize(self, w: int, h: int)
- 改变组件尺寸,即设置组件的width和height属性
- 可以在show方法前初始化,也可以之后再调用
- 注意,并不存在直接设置width和height的方法
setBaseSize貌似无效果,一般未使用
(5)hint相关
- 在 widget 有 layout 的情况下,其 sizeHint() 函数返回的是有效值作为其自身实际尺寸的参考;
- sizeHint() 返回的值并不一定会作为 widget 的实际尺寸,因为 widget 的尺寸的决定还有其它因素作用;
(6)变化范围
# 最大宽度和高度
setMaximumWidth(self, maxw: int)
setMaximumHeight(self, maxh: int)
setMaximumSize(self, maxw: int, maxh: int)
setMaximumSize(self, s: QSize)
# 最小宽度和高度
setMinimumWidth(self, minw: int)
setMouseTracking(self, enable: bool)
setMinimumSize(self, minw: int, minh: int)
setMinimumSize(self, s: QSize)
# 固定宽度和高度,不可变
setFixedWidth(self, w: int)
setFixedHeight(self, h: int)
setFixedSize(self, a0: QSize)
setFixedSize(self, w: int, h: int)
(7)geometry几何
frameGeometry(self) -> QRect
geometry(self) -> QRect
normalGeometry(self) -> QRect
restoreGeometry(self, geometry: Union[QByteArray, bytes, bytearray]) -> bool
saveGeometry(self) -> QByteArray
setGeometry(self, a0: QRect)
setGeometry(self, ax: int, ay: int, aw: int, ah: int)
updateGeometry(self)
- QRect包含x和y,width和height数据
4.3 显示
(1)show和hide
- 将组件显示到父容器中,或者从父容器中隐藏。隐藏时,不占据位置,其它组件会填充
- 如果是窗口组件独立存在,需要显式调用show方法才能出现窗口,否则只创建、可使用,但不显示。如果是一般窗口,则默认会直接显示,不需要调用show
- 如果使用hide方法,隐藏组件。则可以使用show方法,将隐藏组件显示
(2)其它显示方法
showFullScreen(self)
showMaximized(self)
showMinimized(self)
showNormal(self)
isFullScreen(self) -> bool
isMaximized(self) -> bool
isMinimized(self) -> bool
isHidden(self) -> bool
(3)关闭和销毁
close(self) -> bool
destroy(self, destroyWindow: bool = True, destroySubWindows: bool = True)
- close一般只是将窗口隐藏,而窗口实体实际上还存在于内存中,等待再次显示。通过某个设置,也可以使其在关闭后进行销毁
- destroy则销毁窗体。默认destroyWindow为True,即关闭所在窗口;默认destroySubWindows也为True,即关闭打开的子窗口
- 下面的代码,窗口关闭后,休眠结束仍然可以再直接显示
self.window.close()
time.sleep(3)
self.window.show()
(4)update
update(self)
update(self, QRect)
update(self, QRegion)
update(self, int, int, int, int)
- 其具有四种调用方法,默认效果是进行组件的更新显示,某些组件设置可能存在无法立即刷新的效果,可以调用该方法,不用传参
- 也可以设置组件的位置和大小。其位置是相对于父组件进行设置
4.4 窗口、窗口标题和窗标
(1)window
- 返回当前组件的窗口对象,如果本身就是窗口,则返回本身对象
- 比如QMainWindow对象调用该对象,返回类型即QMainWindow
(2)setWindowTitle
- 设置窗口标题,参数为str类型
(3)windowTitle
- 获取窗口标题,返回类型为str类型。
- 如果没有个性设置,QMainWindow的默认窗口标题就为MainWindow
(4)setWindowIcon
setWindowIcon(self, QIcon)
- 设置窗口图标,参数类型是QIcon
- 注意这里QIcon类型对象的创建可能存在问题,加载崩溃,待解决?
(5)windowIcon
- 返回窗口图标,PyQt5.QtGui.QIcon类型
- 如果没有手动设置,则会返回默认的窗口图标,而非None
4.5 其它
(1)setToolTip
- 设置工具提示文本,参数为str
- 一般组件设置后,将鼠标放到组件上,会提示设置的文本。经测试,QMainWindow对象可以设置和获取tooltip,但是不会显示
(2)toolTip
- 获取工具提示文本,返回类型为str
5、事件
# 右键菜单
customContextMenuRequested(self, pos: QPoint) [signal]
# 窗口图标改变
windowIconChanged(self, icon: QIcon) [signal]
# 窗口图标文本改变
windowIconTextChanged(self, iconText: str) [signal]
# 窗口标题改变
windowTitleChanged(self, title: str) [signal]
- QWidget尤其只有这四个事件,值得注意的是,clicked点击事件并不在其中
6、直接的窗口
QWidget可以直接创建,作为主窗口,其没有菜单栏和工具栏。