Search code examples
pythonmayaqgraphicsviewqgraphicsscenepyside2

QRectF doesn't appear in my QGraphicsScene


I am trying to play around with QGraphicsView(inside Maya) and got some code which I will paste down bellow. Problem is that window with QGraphicsView coming , but looks like QGraphicsScene(with my QRectF) doesn't come. I am a little bit still confused how inheritance works , so could somebody point out please where do I do mistake. Thank you.

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

Solution

  • The problem is that you have not added the QGraphicsScene to the 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)