3

我试图将 QGraphicsPixmapItem 放入 QGraphicsLinearLayout。由于这是不可能的,因为 QGraphicsPixmapItem 不是 QGraphicsLayoutItem,所以我试图将后者子类化,并使其像像素图项一样工作。

这是我失败的尝试:

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_())

从评论中可以明显看出, graphicsItem() 方法没有被调用,我最终得到了一个超白的乳白色图形视图。

亲爱的 Qt 科学家,我该如何实现这一点。

4

1 回答 1

2

graphicsItem不是虚拟的。这就是 Qt 不调用它的原因。看来您需要调用setGraphicsItem构造函数并从类中删除graphicsItemsetGraphicsItem方法。重新实现非虚函数没有任何意义。

于 2013-07-01T09:24:26.017 回答