Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Updating QGraphicsEffect for QGraphicsSvgItem
Forum Updated to NodeBB v4.3 + New Features

Updating QGraphicsEffect for QGraphicsSvgItem

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    S Tozer
    wrote on last edited by S Tozer
    #1

    Hi. I have a class derived from QGraphicsSvgItem, which I'd like to be able to apply graphical effects to during selection of these items in my scene.

    With QGraphicsItems, I've been able to re-implement the 'paint' method, and do something during 'isSelected()' - such as changing pen or brush colours. No big deal...

    With QGraphicsSvgItem, I've gotten stuck in understanding how to re-implement the paint method (I haven't been able to get my graphic to display at all once I've added this method to my class). My question is: what code do I need to add after re-implementing 'paint' in order to get the graphic to display? Am I maybe trying to go about this the wrong way?

    Here's some example code:

    from PyQt5.QtWidgets import *
    from PyQt5.QtSvg import *
    from PyQt5.QtCore import *
    
    
    class MySVG(QGraphicsSvgItem):
        def __init__(self, icon):
            super().__init__(icon)
    
            self.setFlags(QGraphicsSvgItem.ItemIsSelectable | QGraphicsSvgItem.ItemSendsGeometryChanges)
    
        # when I uncomment the code below, svg image doesn't show. I'm confused about what I need to write here...
    
        # def paint(self, painter, option, widget=None):
        #
        #     # I want to set a graphics colorize effect when item is selected, and disable the effect when deselected
        #     if self.isSelected():
        #         self._effect = QGraphicsColorizeEffect()
        #         self._effect.setColor(Qt.red)
        #         self._effect.setStrength(.5)
        #         self.setGraphicsEffect(self._effect)
        #
        #     else:
        #         pass
        #         #self.setGraphicsEffect(0)
    
    svg_icon_path = 'E:\\dev\\qt_aStar\\SVG\\input.svg'
    
    app = QApplication([])
    scene = QGraphicsScene()
    view = QGraphicsView(scene)
    
    svg_item = MySVG(svg_icon_path)
    scene.addItem(svg_item)
    
    view.show()
    app.exec_()
    
    
    
    

    Any advice would be much appreciated,
    Thanks!

    Stu

    1 Reply Last reply
    0
    • S Offline
      S Offline
      S Tozer
      wrote on last edited by S Tozer
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • S Offline
        S Offline
        S Tozer
        wrote on last edited by
        #3

        Thanks Denni. I'm still confused about what I need to be passing to what, unfortunately (I'm quite new to QGraphics scenes/views/and items). I've edited my code example above, simplifying it and making it a more complete (runnable) example.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          S Tozer
          wrote on last edited by S Tozer
          #4

          @Denni-0 said in Updating QGraphicsEffect for QGraphicsSvgItem:

          Denni, that's great! Thanks so much. :):) I had tried -

           def paint(self, painter, option, widget=None):
              self.paint(self, painter, option, widget)
          

          Which looked kindof ridiculous (I was just calling the same method on itself, whereas I had to use the actual parent object as you tried to explain to me in previous post).

          Only one thing - my code for disabling the graphicsEffect when my item is deselected doesn't appear to work -

          self.setGraphicsEffect(0)
          

          I'm sure this is no big deal and I will dig around a bit for the answer.

          Thanks again for your time and help on this -

          Stu

          1 Reply Last reply
          0
          • S Offline
            S Offline
            S Tozer
            wrote on last edited by
            #5

            Thanks and apologies.

            I was referring to setting the graphics effect in the 'paint' method on selection of the item, or disabling it once the item is deselected.

            I ended up going with, in the constructor:

                    self._colourise_effect = QGraphicsColorizeEffect()
                    self.setGraphicsEffect(self._colourise_effect)
                    self._colourise_effect.setEnabled(False)
            

            and in the 'paint' method:

                    if self.isSelected():
                        self._colourise_effect.setEnabled(True)
                    else:
                        self._colourise_effect.setEnabled(False)
            

            'self.setGraphicsEffect(0)' or 'self.setGraphicsEffect(None) was not working... (although I had seen this in other code snippets)

            Cheers!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved