Search code examples
pythonpyside2

How to hide specific items in QGraphicScene?


My problem, I want to "hide/show","change color" some items depending on zoom level in my Ui, but I am completely lost in all C+ forums , since my C+ knowledge close to 0. Here is some code:

from PySide2 import QtGui, QtCore, QtWidgets
from PySide2.QtCore import Signal

class testUi(QtWidgets.QDialog):
    def __init__(self, parent=None):
        super(testUi, self).__init__(parent)
        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_signal = Signal(bool)
    def __init__(self, parent=None):
        super(test, self).__init__(parent)
        self._scene = QtWidgets.QGraphicsScene()
        self.__zoom = 0
        self.setScene(self._scene)
        self.drawSomething( )
        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 drawSomething(self):
        path = QtGui.QPainterPath( )
        path.moveTo(0, 0)
        path.addRect(20, 20, 60, 60)
        item = self._scene.addPath(path)
        item.setPen(QtGui.QPen(QtGui.QColor(79, 106, 25), 1, QtCore.Qt.SolidLine, QtCore.Qt.FlatCap, QtCore.Qt.MiterJoin))
        item.setBrush(QtGui.QColor(122, 163, 39))


    def wheelEvent(self, event):
        if event.angleDelta().y() > 0:
            factor = 1.25
            self.__zoom += 1
        else:
            factor = 0.8
        if self.__zoom > 0:
            self.scale(factor, factor)
        if self.__zoom > 10:
            self.zoom_signal.emit(True)
        elif self.__zoom < 10:
            self.zoom_signal.emit(False)
        else:
            self.__zoom = 0



if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = testUi()
    window.setGeometry(500, 300, 800, 600)
    window.show()
    sys.exit(app.exec_())

So I have some "wheelEvent" which helps me to zoom in/out QGraphicScene, where I have "Rect". I want for now to hide "Rect" , if zoom is too big. Thanks for your time.

P.S I know, here is few guys always helping me with understanding step by step Qt and spoon feeding me like a baby, I really appreciate it! I am a bit dummy in this thing.


Solution

  • With your code the variable self.__zoom will never be greater than 10, let's analyze what I point out: initially the value is 0 assuming that it rises one by one when it reaches the value of nine and now the expression else: self .__ zoom = 0 will cause the value to reset to 0, so the value in the best case will go from 0 to 9 and will be reset. So that else is unnecessary. If we remove, the variable can not be reduced, so the value should be reduced when the delta is negative.

    To make the item visible with respect to the zoom_signal signal, it must be connected to the setVisible() method.

    Considering the above the solution is:

    def drawSomething(self):
        path = QtGui.QPainterPath( )
        path.moveTo(0, 0)
        path.addRect(20, 20, 60, 60)
        item = self._scene.addPath(path)
        item.setPen(QtGui.QPen(QtGui.QColor(79, 106, 25), 1, QtCore.Qt.SolidLine, QtCore.Qt.FlatCap, QtCore.Qt.MiterJoin))
        item.setBrush(QtGui.QColor(122, 163, 39))
        self.zoom_signal.connect(item.setVisible)
    
    
    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_signal.emit(self.__zoom < 10)