2

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()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Vlad
  • 387
  • 3
  • 17

2 Answers2

2

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) 
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
2

Eyllanesc is correct, you've created a QGraphicsScene but you still need to set it to the QGraphicsView.

Looking at the docs for QGraphicsView's constructor, you can also pass the scene through one of its __init__ parameters: QGraphicsView.__init__ (self, QGraphicsScene scene, QWidget parent = None)

So you can save a line and set it pass it directly to your class's 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) 
Green Cell
  • 4,677
  • 2
  • 18
  • 49