QRectF没有出现在我的QGraphicsScene中

问题描述 投票:2回答:2

我正在尝试使用QGraphicsView(Maya内部)并获得一些代码,我将在下面粘贴。问题是QGraphicsView即将到来的窗口,但看起来像QGraphicsScene(我的QRectF)并没有来。我对遗传的工作原理有点困惑,所以有人可以指出我在哪里做错了。谢谢。

from PySide2 import QtGui, QtCore, QtWidgets
from shiboken2 import wrapInstance
import maya.OpenMaya as om
import maya.OpenMayaUI as omui
import maya.cmds as cmds
import os, functools


def getMayaWindow():
    pointer = omui.MQtUtil.mainWindow()
    if pointer is not None:
        return wrapInstance(long(pointer), QtWidgets.QWidget)


class testUi(QtWidgets.QDialog):
    def __init__(self, parent=None):  
        if parent is None:
            parent = getMayaWindow()
        super(testUi, self).__init__(parent) 
        self.window = 'vl_test'
        self.title = 'Test Remastered'
        self.size = (1000, 650)

        self.create() 

    def create(self):
        if cmds.window(self.window, exists=True):
            cmds.deleteUI(self.window, window=True)

        self.setWindowTitle(self.title)
        self.resize(QtCore.QSize(*self.size))
        self.testik = test(self)  

        self.mainLayout = QtWidgets.QVBoxLayout() 
        self.mainLayout.addWidget(self.testik)
        self.setLayout(self.mainLayout) 


class test(QtWidgets.QGraphicsView):

    def __init__(self, parent=None):
        super(test, self).__init__(parent) 

        self._scene = QtWidgets.QGraphicsScene() 
        rect_item = QtWidgets.QGraphicsRectItem(QtCore.QRectF(0, 0, 100, 100))
        rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
        self._scene.addItem(rect_item) 

v = testUi()
v.show()
python maya qgraphicsview qgraphicsscene pyside2
2个回答
2
投票

问题是你没有将QGraphicsScene添加到QGraphicsView:

class test(QtWidgets.QGraphicsView):
    def __init__(self, parent=None):
        super(test, self).__init__(parent) 
        self._scene = QtWidgets.QGraphicsScene() 
        self.setScene(self._scene) # <---
        rect_item = QtWidgets.QGraphicsRectItem(QtCore.QRectF(0, 0, 100, 100))
        rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
        self._scene.addItem(rect_item) 

2
投票

Eyllanesc是正确的,你创造了一个QGraphicsScene,但你仍然需要将它设置为QGraphicsView

查看QGraphicsView's constructor的文档,您还可以通过其中一个__init__参数传递场景:QGraphicsView.__init__ (self, QGraphicsScene scene, QWidget parent = None)

所以你可以保存一行并设置它直接传递给你的班级的super

class test(QtWidgets.QGraphicsView):

    def __init__(self, scene, parent=None):
        self._scene = QtWidgets.QGraphicsScene()  # Create scene first.

        super(test, self).__init__(self._scene, parent)  # Pass scene to the QGraphicsView's constructor method.

        rect_item = QtWidgets.QGraphicsRectItem(QtCore.QRectF(0, 0, 100, 100))
        rect_item.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
        self._scene.addItem(rect_item) 
© www.soinside.com 2019 - 2024. All rights reserved.