Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 子类化QGraphicsLayoutItem以显示pixmap_Python_Qt_Pyside - Fatal编程技术网

Python 子类化QGraphicsLayoutItem以显示pixmap

Python 子类化QGraphicsLayoutItem以显示pixmap,python,qt,pyside,Python,Qt,Pyside,我试图将QGraphicsPixmapItem放入QGraphicsLinearLayout中。 因为这是不可能的,因为QGraphicsPixmapItem不是QGraphicsLayoutItem, 我试图对后者进行子类化,使其像pixmap项目一样工作 以下是我失败的尝试: from PySide import QtGui, QtCore import sys class MyItem(QtGui.QGraphicsLayoutItem): def __init__(self, im

我试图将QGraphicsPixmapItem放入QGraphicsLinearLayout中。 因为这是不可能的,因为QGraphicsPixmapItem不是QGraphicsLayoutItem, 我试图对后者进行子类化,使其像pixmap项目一样工作

以下是我失败的尝试:

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科学家们,我该如何实现这一点。

graphicsItem
不是虚拟的。这就是为什么Qt不叫它。似乎需要在构造函数中调用
setGraphicsItem
,并从类中删除
graphicsItem
setGraphicsItem
方法。重新实现非虚拟函数没有任何意义。

从构造函数调用setGraphicsItem将如何改变任何事情?。如果不调用graphicsItem,系统如何知道类所表示的项?。在setGraphicsItem中是否有特殊的魔法?系统调用
graphicsItem()
的默认实现,它返回以前使用
setGraphicsItem
的默认实现设置的项。只需调用self.setGraphicsItem(self.gitem)。好的。我刚刚删除了
setGraphicsItem
graphicsItem
,并按照您所说的做了,看起来效果不错。谢谢。