Issue
I was trying to have a QGraphicsPixmapItem into a QGraphicsLinearLayout. Since that is not possible, because QGraphicsPixmapItem is not a QGraphicsLayoutItem, I am trying to subclass the latter, and make it work like a pixmap item.
Here's my failing attempt:
from PySide import QtGui, QtCore
import sys
class MyItem(QtGui.QGraphicsLayoutItem):
def __init__(self, image, parent=None):
super().__init__(parent)
self.gitem = QtGui.QGraphicsPixmapItem()
self.gitem.setPixmap(QtGui.QPixmap(image))
def sizeHint(which, constraint, other):
return QtCore.QSizeF(200, 200)
def setGeometry(self, rect):
self.gitem.setPos(rect.topLeft())
def graphicsItem(self):
#does not get called
return self.gitem
def setGraphicsItem(self, item):
self.gitem = item
class Application(QtGui.QGraphicsView):
def __init__(self, parent=None):
super().__init__(parent)
gwidget = QtGui.QGraphicsWidget()
layout = QtGui.QGraphicsLinearLayout()
layout.addItem(MyItem(r'C:\image1.jpg'))
layout.addItem(MyItem(r'C:\image2.jpg'))
gwidget.setLayout(layout)
scene = QtGui.QGraphicsScene()
scene.addItem(gwidget)
self.setScene(scene)
app = QtGui.QApplication(sys.argv)
main = Application()
main.show()
sys.exit(app.exec_())
And as evident from the comment, the graphicsItem() method does not get called, and I end up with a super-white milky graphics view.
How do I achieve this, dear Qt scientists.
Solution
graphicsItem
is not virtual. That's why Qt doesn't call it. It seems you need to call setGraphicsItem
in your constructor and remove graphicsItem
and setGraphicsItem
methods from your class. Reimplementing non-virtual functions doesn't make any sense.
Answered By - Pavel Strakhov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.