gpt4 book ai didi

Qt/PyQt : QGraphicsItem vs. QGraphicsWidget 几何、位置、鼠标交互

转载 作者:行者123 更新时间:2023-12-04 08:08:42 28 4
gpt4 key购买 nike

我正在将更大的 QGraphicsItems 程序转换为 QGraphicsWidgets(为了打字,我们称它们为 item 和 widget)。鼠标悬停现在失败,因为小部件的位置和/或矩形与旧项目不同。我已经归结为一个简单的案例,其中包含一个 View 、场景、一个项目和一个小部件。蓝色项目以 100x50 像素呈现,并按预期发生 hoverEnterEvent。但是,红色小部件以预期宽度的一半呈现。如果我为小部件重新实现纯虚函数 boundingRect,我可以解决这个问题,但悬停事件仍然只在 50x50 左半部分上触发。我需要使用/覆盖哪些 pos/rect/geometry 方法才能使小部件像项目一样与鼠标正确交互?谢谢。这是我的示例代码

#!/usr/local/bin/python

import os, sys
from PyQt4.Qt import *
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyView(QGraphicsView):
def __init__(self):
QGraphicsView.__init__(self)
self.setWindowFlags(Qt.WindowStaysOnTopHint)
self.scene = QGraphicsScene(self)
self.item = GraphicsItem('item', 100, 50)
self.item.moveBy(50, 50)
self.scene.addItem(self.item)
self.widget = GraphicsWidget('widget', 100, 50)
self.scene.addItem(self.widget)
self.setScene(self.scene)

class GraphicsItem(QGraphicsItem):
def __init__(self, name, width, height):
QGraphicsItem.__init__(self)
self.setAcceptHoverEvents(True)
self.name = name
self.__width = width
self.__height = height

def boundingRect(self):
return QRectF(0, 0, self.__width, self.__height)

def hoverEnterEvent(self, event):
self.__printGeometryDetails()

def paint(self, painter, option, widget):
bgRect = self.boundingRect()
painter.drawRects(bgRect)
painter.fillRect(bgRect, QColor('blue'))

def __printGeometryDetails(self):
print self.name
print ' pos (%.0f, %0.0f)' % (self.pos().x(), self.pos().y())
print ' boundingRect (%.0f, %0.0f, %.0f, %0.0f)' % (self.boundingRect().x(), self.boundingRect().y(), self.boundingRect().width(), self.boundingRect().height())

class GraphicsWidget(QGraphicsWidget):
def __init__(self, name, width, height):
QGraphicsWidget.__init__(self)
self.setAcceptHoverEvents(True)
self.name = name
self.__width = width
self.__height = height

def boundingRect(self):
return QRectF(0, 0, self.__width, self.__height)

def hoverEnterEvent(self, event):
self.__printGeometryDetails()

def paint(self, painter, option, widget):
bgRect = self.boundingRect()
painter.drawRects(bgRect)
painter.fillRect(bgRect, QColor('red'))

def __printGeometryDetails(self):
print self.name
print ' pos (%.0f, %0.0f)' % (self.pos().x(), self.pos().y())
print ' boundingRect (%.0f, %0.0f, %.0f, %0.0f)' % (self.boundingRect().x(), self.boundingRect().y(), self.boundingRect().width(), self.boundingRect().height())
print ' geometry (%.0f, %0.0f, %.0f, %0.0f)' % (self.geometry().x(), self.geometry().y(), self.geometry().width(), self.geometry().height())
print ' rect (%.0f, %0.0f, %.0f, %0.0f)' % (self.rect().x(), self.rect().y(), self.rect().width(), self.rect().height())

if __name__ == '__main__':
app = QApplication(sys.argv)
view = MyView()
view.setGeometry(600, 100, 400, 370)
view.show()
sys.exit(app.exec_())

最佳答案

如果您使用 self.resize(width, height),它似乎可以正常工作而不是重新定义 boundingRect .

关于Qt/PyQt : QGraphicsItem vs. QGraphicsWidget 几何、位置、鼠标交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8044024/

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