5

我正在将一个更大的 QGraphicsItems 程序转换为 QGraphicsWidgets(为了打字,我们称它们为项目和小部件)。鼠标悬停现在失败,因为小部件的位置和/或矩形与旧项目不同。我已经归结为一个包含视图、场景、项目和小部件的简单案例。蓝色项目以 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_())
4

1 回答 1

5

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

于 2011-11-08T01:16:11.140 回答