Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt/PyQt: QGraphicsItem vs. QGraphicsWidget geometry, position, mouse interaction

I am converting a larger program of QGraphicsItems to QGraphicsWidgets (let's call them item and widget for typing sake). Mouse hover fails now because the position and/or rect of the widgets are not the same as the old items. I've boiled down to a simple case with a view, scene, an item and a widget. The blue item renders at 100x50 pix, and hoverEnterEvent occurs as expected. However, the red widget is rendered at half intended width. I can fix this if I reimplement the pure virtual function boundingRect for the widget but the hover event is still only triggered atop the 50x50 left half. What pos/rect/geometry methods do I need to use/override to get the widget to interact properly with the mouse just like the item? Thanks. Here's my sample code

#!/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_())
like image 881
user1034632 Avatar asked Nov 07 '11 23:11

user1034632


1 Answers

It does seem to work correctly if you use self.resize(width, height) instead of redefining boundingRect.

like image 133
alexisdm Avatar answered Oct 17 '22 04:10

alexisdm