gpt4 book ai didi

python - PyQt : How to set a mask over a QWidget to ignore mouse events but get the children events

转载 作者:太空宇宙 更新时间:2023-11-03 14:36:12 25 4
gpt4 key购买 nike

我想要重叠小部件并使它们全部发挥作用,但我无法通过简单的布局管理来完成此任务。我找到了一个在小部件上应用掩码的示例,但它是用 C++ 编写的,我很难尝试将其移植到 python

示例在本文中:

这正是我所需要的,我正在尝试在我的代码中实现它。但我收到错误:

NotImplementedError: reverse operator not implemented

我该如何解决这个问题?

我在代码中所做的一个非常简单的示例:

from PySide import QtCore as qc
from PySide import QtGui as qg
import operator
###############################################################
class WinDin(qg.QDialog):
def __init__(self):
super(WinDin, self).__init__()
self.setLayout(qg.QVBoxLayout())
self.setFixedSize(500,500)

button_00 = qg.QPushButton('death')
self.layout().addWidget(button_00)

##################################################
# IM USING A QLABEL FOR EASY LOOK THROUGH ...
#
self._mainWidget = qg.QLabel(self)
self._mainWidget.setFixedSize(500,500)
self._mainWidget.setStyleSheet("background-color: rgba(255,0,0,5)")

lay_out = qg.QHBoxLayout(self._mainWidget)
button_01 = qg.QPushButton('trick')

lay_out.setAlignment(qc.Qt.AlignBottom)
lay_out.addWidget(button_01)

self.settingMask()
############################################################################################################################################
def settingMask(self):
### SAW THIS EXAMPLE IN OTHER FORO IN C++

frame_geometry = self._mainWidget.frameGeometry()
wd_geo = self._mainWidget.geometry()
child_reg = self._mainWidget.childrenRegion()


##############################
# TESTING WITH OPERATOR MODULE I GET AND ERRROR . 'PySide.QtCore.QRect' is not iterable
#
#this = operator.__contains__(wd_geo,wd_geo)
#self._mainWidget.setMask(this)

self._mainWidget.setMask(frame_geometry - child_reg)

###
###
### THIS WAY I GET : NotImplementedError: reverse operator not implemented.

##########################################
def main():
import sys
qtApp=qg.QApplication(sys.argv)
myWinPos=WinDin()
myWinPos.show()
sys.exit(qtApp.exec_())

if __name__=="__main__":
main()

如何从几何体中减去几何体并使其正常工作?

最佳答案

您需要使用 QRegion 类才能使其正常工作:

def settingMask(self):
region = qg.QRegion(self._mainWidget.frameGeometry())
region -= qg.QRegion(self._mainWidget.geometry())
region += self._mainWidget.childrenRegion()
self._mainWidget.setMask(region)

此外,在我的系统 (Linux) 上,我发现我必须在窗口显示后调用此函数:

def main():
...
myWinPos.show()
myWinPos.settingMask()
sys.exit(qtApp.exec_())

关于python - PyQt : How to set a mask over a QWidget to ignore mouse events but get the children events,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46940966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com