Can't make custom QGraphicsItem, getting ObjectType

Question:

Maybe a week ago I was asking about how to inherit and make custom classes. As it was explained you have to inherit from the module, for example in my case it is QtWidgets.QGraphicsItem and you need to call it with super or QtWidgets.QGraphicsItem.__init__(self) like this. I’ve read some stuff about it and I was playing around with it quite a bit and it was working , but when today I tried QtWidgets.QGraphicsItemit , it didn’t work. Here is my code:


        from PySide2 import QtGui, QtCore, QtWidgets
    
    class testUi(QtWidgets.QDialog):
        def __init__(self, parent=None):
            QtWidgets.QDialog.__init__(self)
            self.window = 'vl_test'
            self.title = 'Test Remastered'
            self.size = (1000, 650)
    
            self.create( )
    
        def create(self):
            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):
        zoom_signalA = QtCore.Signal(bool)
        zoom_signalB = QtCore.Signal(bool)
        def __init__(self, parent=None):
            QtWidgets.QGraphicsView.__init__(self)
            self._scene = QtWidgets.QGraphicsScene(backgroundBrush=QtCore.Qt.gray)
            self.__zoom = 0
            self.setScene(self._scene)
    
            self.graphicsItm = self._scene.addItem(graphicButton)
    
            self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
            self.setResizeAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
            self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
            self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
            self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(30, 30, 30)))
            self.setFrameShape(QtWidgets.QFrame.NoFrame)
            self.setSizePolicy(QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding))
    
    
    
        def wheelEvent(self, event):
            if event.angleDelta( ).y( ) > 0:
                factor = 1.25
                self.__zoom += 1
            else:
                factor = 0.8
                self.__zoom -= 1
            self.scale(factor, factor)
            #self.zoom_signalA.emit(self.__zoom < 5)
            #self.zoom_signalB.emit(self.__zoom > 4)
    
    
    class graphicButton(QtWidgets.QGraphicsItem):
        def __init__(self):
            QtWidgets.QGraphicsItem.__init__(self)
            pixmap = QtGui.QPixmap(r"C:Usersv.afanasjevsDesktopgimpTestgimpTestButtonPresedA.png")
            pixmap_item = QtWidgets.QGraphicsPixmapItem(pixmap)
            pixmap_item.setFlags(
                pixmap_item.flags( )
                | QtWidgets.QGraphicsItem.ItemIsSelectable
                | QtWidgets.QGraphicsItem.ItemIsMovable
            )
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        window = testUi()
        window.setGeometry(500, 300, 800, 600)
        window.show()
        sys.exit(app.exec_())

Error :

    TypeError: 'PySide2.QtWidgets.QGraphicsScene.addItem' called with wrong argument types:
      PySide2.QtWidgets.QGraphicsScene.addItem(ObjectType)
    Supported signatures:
      PySide2.QtWidgets.QGraphicsScene.addItem(PySide2.QtWidgets.QGraphicsItem)

My guess is that I do mistake thinking that you can make everything some custom widget, like I am doing here, trying to make QtGui.QPixmap a graphic item. If , I am right and I am doing it wrong , then how to use virtual functions of QGraphicsItem , on my QPixmap ? (I want to make my image a button , so I guess I have to use "mousePressEvent" and etc).
Thank you for your time.

Asked By: Vlad

||

Answers:

graphicButton is the name of your class.

By doing this self.graphicsItm = self._scene.addItem(graphicButton), you pass the class as parameter.

Change it to self.graphicsItm = self._scene.addItem(graphicButton()) to create a new instance.

Your graphicButton has to override QGraphicsItem::paint method.

But, it could also inherit from QGraphicsPixmapItem:

class GraphicButton(QtWidgets.QGraphicsPixmapItem):
    def __init__(self):
        pixmap = QtGui.QPixmap(r"img.png")
        QtWidgets.QGraphicsPixmapItem.__init__(self, pixmap)
        self.setFlags(
            self.flags( )
            | QtWidgets.QGraphicsItem.ItemIsSelectable
            | QtWidgets.QGraphicsItem.ItemIsMovable
        )
Answered By: Dimitry Ernot
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.