QRectF 没有出现在我的 QGraphicsScene 中



我正在尝试使用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()

問題是你沒有將 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) 

> Eyllanesc 是正确的,您已经创建了一个QGraphicsScene但仍需要将其设置为 QGraphicsView

查看 QGraphicsView 构造函数的文档,您还可以通过其__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) 

相关内容

  • 没有找到相关文章

最新更新