pyqt "ItemIgnoresParentOpacity" 标记被忽略
pyqt "ItemIgnoresParentOpacity" Flag being ignored
我已经花时间为我的问题做了一个最小可重现的例子。
使用 PyQt5,我有一个经典的 Main window,其中 QGraphicsView 链接到 QGraphicsScene。
在现场,我有一个 NodeHover
项(绿色圆圈)和一个 TreeNode
子项(正方形)。
NodeHover 项目默认是透明的,悬停时变为不透明。
我希望其子项 (TreeNode
) 始终保持不透明,无论父项的不透明度如何。我已经尝试在这两个项目中设置标志,如下所示,但它们似乎不起作用。
import sys
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QMainWindow, QApplication, QToolBar, QStatusBar, QAction, QGraphicsItem, QGraphicsLineItem, QLabel, QGraphicsEllipseItem, QGraphicsTextItem
from PyQt5.QtCore import *
from PyQt5.QtGui import QPainter, QIcon, QPen, QBrush, QTransform
class NodeHover(QGraphicsItem):
def __init__(self, *args, **kwargs):
super(NodeHover, self).__init__(*args, **kwargs)
self.setOpacity(0.1)
self.setAcceptHoverEvents(True)
# Setting the flag so Item doesn't propagate its opacity to children
self.GraphicsItemFlag(QGraphicsItem.ItemDoesntPropagateOpacityToChildren)
def hoverEnterEvent(self, event):
self.setOpacity(1) # When mouse enters item, make it opaque
def hoverLeaveEvent(self, event):
self.setOpacity(0.1) # When mouse leaves item, make it transparent
def boundingRect(self):
return QRectF(0,0, 20, 20)
def paint(self, painter, option, widget):
painter.setBrush(QBrush(Qt.green))
painter.drawEllipse(QPointF(10, 10), 10, 10)
class TreeNode(QGraphicsItem):
def __init__(self, *args, **kwargs):
super(TreeNode, self).__init__(*args, **kwargs)
# Setting the flag to ignore parent's opacity
self.GraphicsItemFlag(QGraphicsItem.ItemIgnoresParentOpacity)
def boundingRect(self):
return QRectF(100, 100, 100, 100)
def paint(self, painter, option, widget):
painter.drawRect(110,110,80,80)
class GView(QGraphicsView):
def __init__(self, parent, *args, **kwargs):
super().__init__(*args, **kwargs)
self.parent = parent
self.setGeometry(100, 100, 700, 450)
self.show()
class Scene(QGraphicsScene):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
hoverItem = NodeHover() # create a NodeHover item
self.addItem(hoverItem)
nodeItem = TreeNode() # create a TreeNode item and make it hoverItem's child
nodeItem.setParentItem(hoverItem)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(900, 70, 1000, 800)
self.createGraphicView()
self.show()
def createGraphicView(self):
self.scene = Scene(self)
gView = GView(self)
scene = Scene(gView)
gView.setScene(scene)
# Set the main window's central widget
self.setCentralWidget(gView)
# Run program
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
要为 QGraphicsItem 设置标志,您必须使用 setFlag()
方法:
self.setFlag(QGraphicsItem.ItemDoesntPropagateOpacityToChildren)
self.setFlag(QGraphicsItem.ItemIgnoresParentOpacity)
注意:其中一个标志就足够了。
我已经花时间为我的问题做了一个最小可重现的例子。
使用 PyQt5,我有一个经典的 Main window,其中 QGraphicsView 链接到 QGraphicsScene。
在现场,我有一个 NodeHover
项(绿色圆圈)和一个 TreeNode
子项(正方形)。
NodeHover 项目默认是透明的,悬停时变为不透明。
我希望其子项 (TreeNode
) 始终保持不透明,无论父项的不透明度如何。我已经尝试在这两个项目中设置标志,如下所示,但它们似乎不起作用。
import sys
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QMainWindow, QApplication, QToolBar, QStatusBar, QAction, QGraphicsItem, QGraphicsLineItem, QLabel, QGraphicsEllipseItem, QGraphicsTextItem
from PyQt5.QtCore import *
from PyQt5.QtGui import QPainter, QIcon, QPen, QBrush, QTransform
class NodeHover(QGraphicsItem):
def __init__(self, *args, **kwargs):
super(NodeHover, self).__init__(*args, **kwargs)
self.setOpacity(0.1)
self.setAcceptHoverEvents(True)
# Setting the flag so Item doesn't propagate its opacity to children
self.GraphicsItemFlag(QGraphicsItem.ItemDoesntPropagateOpacityToChildren)
def hoverEnterEvent(self, event):
self.setOpacity(1) # When mouse enters item, make it opaque
def hoverLeaveEvent(self, event):
self.setOpacity(0.1) # When mouse leaves item, make it transparent
def boundingRect(self):
return QRectF(0,0, 20, 20)
def paint(self, painter, option, widget):
painter.setBrush(QBrush(Qt.green))
painter.drawEllipse(QPointF(10, 10), 10, 10)
class TreeNode(QGraphicsItem):
def __init__(self, *args, **kwargs):
super(TreeNode, self).__init__(*args, **kwargs)
# Setting the flag to ignore parent's opacity
self.GraphicsItemFlag(QGraphicsItem.ItemIgnoresParentOpacity)
def boundingRect(self):
return QRectF(100, 100, 100, 100)
def paint(self, painter, option, widget):
painter.drawRect(110,110,80,80)
class GView(QGraphicsView):
def __init__(self, parent, *args, **kwargs):
super().__init__(*args, **kwargs)
self.parent = parent
self.setGeometry(100, 100, 700, 450)
self.show()
class Scene(QGraphicsScene):
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
hoverItem = NodeHover() # create a NodeHover item
self.addItem(hoverItem)
nodeItem = TreeNode() # create a TreeNode item and make it hoverItem's child
nodeItem.setParentItem(hoverItem)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(900, 70, 1000, 800)
self.createGraphicView()
self.show()
def createGraphicView(self):
self.scene = Scene(self)
gView = GView(self)
scene = Scene(gView)
gView.setScene(scene)
# Set the main window's central widget
self.setCentralWidget(gView)
# Run program
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
要为 QGraphicsItem 设置标志,您必须使用 setFlag()
方法:
self.setFlag(QGraphicsItem.ItemDoesntPropagateOpacityToChildren)
self.setFlag(QGraphicsItem.ItemIgnoresParentOpacity)
注意:其中一个标志就足够了。