widget doesn't fit inside QGraphicsProxyWidget
Hello community !
I am trying to create a custom QGraphicsProxtWidget with a predefined size then set its widget. But the widget doesn't match the proxy's size.
Isn't the widget supposed to inherit from the proxy ? Or I am missing a step ?
Here is my code so far :
from PySide import QtGui, QtCore class GraphicItem(QtGui.QGraphicsItem): def __init__(self, parent=None): super(GraphicItem, self).__init__(parent) self.setFlag(self.ItemIsMovable) self.setFlag(self.ItemIsSelectable) def boundingRect(self): rect = QtCore.QRect(0,0,400,400) return rect def shape(self): path = QtGui.QPainterPath() path.addRect(self.boundingRect()) return path def paint(self, painter, option, widget): painter.setBrush(QtGui.QColor(255,0,0,255)) painter.drawRect(self.boundingRect()) class ProxyItem(QtGui.QGraphicsProxyWidget): def __init__(self, parent=None): super(ProxyItem, self).__init__(parent) self.x = 300 self.y = 300 def boundingRect(self): rect = QtCore.QRect(0,0,self.x,self.y) return rect def sizeHint(self, option, index): size = QtCore.QSize(self.x,self.y) return size def shape(self): path = QtGui.QPainterPath() path.addRect(self.boundingRect()) return path s = QtGui.QGraphicsScene() v = QtGui.QGraphicsView(s) v.show() ti = GraphicItem(parent=None) s.addItem(ti) pi = ProxyItem(parent=ti) pi.setWidget(QtGui.QTextBrowser()) pi.setPos(5,5)
If somebody has a good example of how to subclass a QGraphicsProxyWidget, I will gladly take it since I can't find one ...
Why create a subclass of QGraphicsProxyWidget ? From your description you should set a fixed size on your widget and add it to the scene.
I'm not entirely sure that I will need the subclass but I might. And even if I don't, I always like to know how to do it just in case I have to in the future.
I'm prototyping something and I'm trying different approaches.
The idea is to have this proxy resized by it's parent. So I wanted my GraphicsItem to resize my proxy thinking that my widget would directly be resized by the proxy.
Subclassing for the sake of subclassing isn't a good idea. The manipulation you plan to do really don't seem to call for any subclassing.
You can manipulate your QGraphicsProxyWidget geometry directly so you seem to over-engineer things a bit here.
but what if I have to change the events ? or add new things to it. It's not because it's not in the example that I won't have to do it.
Depending on what you want to add, it will likely make more sense to modify the underlying widget. But that's up to you to decide.
So for now I'm using QGraphicsProxyWidget without subclassing and directly setting the geometry of my widget.
Now I want to move my GraphicsItem by clicking and moving from the widget. So I have to override or ignore them. But the Widget doesn't really have a parent/child relationship with my QGraphicsItem.
So Can I use the events.ignore() ? Or do I somehow have to get a pointer from my GraphicsItem inside my widget and manually override everything ?
Then you should take a look at the Pad Navigator example.