Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. highlight QGraphicsItem
Forum Updated to NodeBB v4.3 + New Features

highlight QGraphicsItem

Scheduled Pinned Locked Moved Solved General and Desktop
43 Posts 4 Posters 14.0k Views 4 Watching
  • 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.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    how can i highlight a custom QGraphicsItem on hover? the item is a cross - X, drawn by two lines.

    i know about setGraphicsEffect(new QGraphicsColorizeEffect()) but that's not what i want. i want there to be outlines (borders) around the cross on hover.

    i guess i need to sublcass QGraphicsEffect and override its draw(), but how?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      What about just subclass QGraphicsItem and paint it yourself

      class SimpleItem : public QGraphicsItem
      {
      public:
             void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                     QWidget *widget)
          {
          draw the lines / with or without highlight
          }
      };
      
      U 1 Reply Last reply
      1
      • mrjjM mrjj

        Hi
        What about just subclass QGraphicsItem and paint it yourself

        class SimpleItem : public QGraphicsItem
        {
        public:
               void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                       QWidget *widget)
            {
            draw the lines / with or without highlight
            }
        };
        
        U Offline
        U Offline
        user4592357
        wrote on last edited by
        #3

        @mrjj
        there i draw without the highlight. it's okay.
        for hoverEnterEvent i need to draw WITH highlight

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          You can trigger a repaint in the hoverEnterEvent.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          U 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            You can trigger a repaint in the hoverEnterEvent.

            U Offline
            U Offline
            user4592357
            wrote on last edited by
            #5

            @SGaist
            yes, but how should i draw a "border" around the line?

            K 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Paint a rectangle that is slightly bigger that the geometry of your widget.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • U user4592357

                @SGaist
                yes, but how should i draw a "border" around the line?

                K Offline
                K Offline
                kenchan
                wrote on last edited by
                #7

                @user4592357 the same draw function needs to know when it needs to draw the highlight or just the line... how you do that depends on details of your implementation which you have not shared with us yet. I am sure you can figure some way to do it though :-).

                U 1 Reply Last reply
                0
                • K kenchan

                  @user4592357 the same draw function needs to know when it needs to draw the highlight or just the line... how you do that depends on details of your implementation which you have not shared with us yet. I am sure you can figure some way to do it though :-).

                  U Offline
                  U Offline
                  user4592357
                  wrote on last edited by
                  #8

                  @kenchan
                  it is in Python, sorry. my tries are in vain:

                  class Item(QGraphicsItem):
                  
                      def __init__(self, x, y):
                          super(ErrorItem, self).__init__()
                          self.__x = x
                          self.__y = y
                  
                          self.__hovered = False
                  
                          self.setAcceptHoverEvents(True)
                  
                      def paint(self, painter, option, widget):
                          pen = QPen(Qt.red)
                          pen.setWidth(4)
                          painter.setPen(pen)
                  
                          painter.drawLine(QPoint(0, 0), QPoint(100, 100))
                          painter.drawLine(QPoint(0, 100), QPoint(100, 0))
                  
                          if self.__hovered:
                              # what should go here?
                              pen.setColor(Qt.white)
                              pen.setWidth(8)
                              self.__hovered = False
                              # painter.drawLine(QPoint(-50, -50), QPoint(103, 103))
                  
                      def boundingRect(self):
                          return QRectF(0, 0, 100, 100)
                  
                      def hoverEnterEvent(self, event):
                          # self.setGraphicsEffect(QGraphicsColorizeEffect())
                          self.__hovered = True
                          
                      def hoverLeaveEvent(self, event):
                          self.setGraphicsEffect(None)
                  
                  K 1 Reply Last reply
                  0
                  • U user4592357

                    @kenchan
                    it is in Python, sorry. my tries are in vain:

                    class Item(QGraphicsItem):
                    
                        def __init__(self, x, y):
                            super(ErrorItem, self).__init__()
                            self.__x = x
                            self.__y = y
                    
                            self.__hovered = False
                    
                            self.setAcceptHoverEvents(True)
                    
                        def paint(self, painter, option, widget):
                            pen = QPen(Qt.red)
                            pen.setWidth(4)
                            painter.setPen(pen)
                    
                            painter.drawLine(QPoint(0, 0), QPoint(100, 100))
                            painter.drawLine(QPoint(0, 100), QPoint(100, 0))
                    
                            if self.__hovered:
                                # what should go here?
                                pen.setColor(Qt.white)
                                pen.setWidth(8)
                                self.__hovered = False
                                # painter.drawLine(QPoint(-50, -50), QPoint(103, 103))
                    
                        def boundingRect(self):
                            return QRectF(0, 0, 100, 100)
                    
                        def hoverEnterEvent(self, event):
                            # self.setGraphicsEffect(QGraphicsColorizeEffect())
                            self.__hovered = True
                            
                        def hoverLeaveEvent(self, event):
                            self.setGraphicsEffect(None)
                    
                    K Offline
                    K Offline
                    kenchan
                    wrote on last edited by kenchan
                    #9

                    @user4592357 Sorry I don't develop Qt with Python...
                    Maybe you could put the line drawing bits after the if statement and make that just change the colour and the thickness?
                    Since you can draw the line normally then that should work as before, right?
                    The next issue will be if your hovered flag is getting changed when the event is fired. Can you debug that to check it?
                    Finally i guess you will want to force the scene to redraw, if that is the problem you are seeing.
                    I would have thought that the scene would be redrawn when the hover events happen?
                    I believe the default implementation of hoveredEnterEvent() function just calls update(). Maybe you should call update() or something?
                    I think you should reset the hovered flag to false in the hoverLeaveEvent so it only happens when you are actually hovering over it :-)

                    U 1 Reply Last reply
                    0
                    • K kenchan

                      @user4592357 Sorry I don't develop Qt with Python...
                      Maybe you could put the line drawing bits after the if statement and make that just change the colour and the thickness?
                      Since you can draw the line normally then that should work as before, right?
                      The next issue will be if your hovered flag is getting changed when the event is fired. Can you debug that to check it?
                      Finally i guess you will want to force the scene to redraw, if that is the problem you are seeing.
                      I would have thought that the scene would be redrawn when the hover events happen?
                      I believe the default implementation of hoveredEnterEvent() function just calls update(). Maybe you should call update() or something?
                      I think you should reset the hovered flag to false in the hoverLeaveEvent so it only happens when you are actually hovering over it :-)

                      U Offline
                      U Offline
                      user4592357
                      wrote on last edited by
                      #10

                      @kenchan
                      drawing the line thicker isn't a problem, but i don't know how to draw a red line (normal) + the highlight (outline) around it which is white

                      K 2 Replies Last reply
                      0
                      • U user4592357

                        @kenchan
                        drawing the line thicker isn't a problem, but i don't know how to draw a red line (normal) + the highlight (outline) around it which is white

                        K Offline
                        K Offline
                        kenchan
                        wrote on last edited by
                        #11

                        @user4592357
                        if you can draw it one way you can draw it the other way right?
                        So what exactly is the problem now then, the events are not working? the events work but your draw functions is not getting called when expect?

                        U 1 Reply Last reply
                        0
                        • K kenchan

                          @user4592357
                          if you can draw it one way you can draw it the other way right?
                          So what exactly is the problem now then, the events are not working? the events work but your draw functions is not getting called when expect?

                          U Offline
                          U Offline
                          user4592357
                          wrote on last edited by
                          #12

                          @kenchan
                          as you can see in code, i set a boolean in hover event and in paint() check - if it's hovered draw outline, but it doesn't. i also tried to call update() inside hoverEnterEvent - still no luck

                          K 2 Replies Last reply
                          0
                          • U user4592357

                            @kenchan
                            drawing the line thicker isn't a problem, but i don't know how to draw a red line (normal) + the highlight (outline) around it which is white

                            K Offline
                            K Offline
                            kenchan
                            wrote on last edited by
                            #13

                            @user4592357
                            What don't you understand about how to draw it?
                            If you want an outline around a line the easiest way is to draw a thin line on top of a thicker line using the same two points.
                            You draw the thick one first then you draw the thin one on top. If you want to make absolutely sure that the thin one is on top you can set the z value of it to be above the thick one.

                            1 Reply Last reply
                            0
                            • U user4592357

                              @kenchan
                              as you can see in code, i set a boolean in hover event and in paint() check - if it's hovered draw outline, but it doesn't. i also tried to call update() inside hoverEnterEvent - still no luck

                              K Offline
                              K Offline
                              kenchan
                              wrote on last edited by
                              #14

                              @user4592357
                              And does you hovered flag actually get set?

                              U 1 Reply Last reply
                              0
                              • U user4592357

                                @kenchan
                                as you can see in code, i set a boolean in hover event and in paint() check - if it's hovered draw outline, but it doesn't. i also tried to call update() inside hoverEnterEvent - still no luck

                                K Offline
                                K Offline
                                kenchan
                                wrote on last edited by kenchan
                                #15

                                @user4592357
                                Are you adding the pen thickness to your bounding rect?
                                Also, I think you must call prepareGeometyChange() if you change the shape or size of the bounding box etc.
                                You usually call this in a setting function that results in a change in shape. In your case you probably want to do it in your event function.

                                1 Reply Last reply
                                0
                                • K kenchan

                                  @user4592357
                                  And does you hovered flag actually get set?

                                  U Offline
                                  U Offline
                                  user4592357
                                  wrote on last edited by
                                  #16

                                  @kenchan
                                  z value of what? QLine?

                                  K 2 Replies Last reply
                                  0
                                  • U user4592357

                                    @kenchan
                                    z value of what? QLine?

                                    K Offline
                                    K Offline
                                    kenchan
                                    wrote on last edited by
                                    #17

                                    @user4592357
                                    Ah, sorry that is a property of your graphics item :-) no, I think just the draw order should work for the painter.

                                    1 Reply Last reply
                                    0
                                    • U user4592357

                                      @kenchan
                                      z value of what? QLine?

                                      K Offline
                                      K Offline
                                      kenchan
                                      wrote on last edited by
                                      #18

                                      @user4592357
                                      So, did you have any luck with it yet?

                                      U 1 Reply Last reply
                                      0
                                      • K kenchan

                                        @user4592357
                                        So, did you have any luck with it yet?

                                        U Offline
                                        U Offline
                                        user4592357
                                        wrote on last edited by
                                        #19

                                        @kenchan
                                        no this is what i get:

                                        no hover:
                                        alt text

                                        after hover:
                                        alt text

                                        code:

                                            def paint(self, painter, option, widget):
                                                pen = QPen(Qt.red)
                                                if self.__hovered:
                                                    # what should go here?
                                                    self.__hovered = False
                                                    pen.setColor(Qt.white)
                                                    pen.setWidth(8)
                                                    painter.setPen(pen)
                                                    painter.drawLine(QPoint(0, 0), QPoint(100, 100))
                                                    painter.drawLine(QPoint(0, 100), QPoint(100, 0))
                                        
                                                pen.setWidth(4)
                                                painter.setPen(pen)
                                        
                                                line = QLine(QPoint(0, 0), QPoint(100, 100))
                                                painter.drawLine(line)
                                                painter.drawLine(QPoint(0, 100), QPoint(100, 0))
                                        
                                            def boundingRect(self):
                                                return QRectF(0, 0, 108, 108)
                                        
                                            def hoverEnterEvent(self, event):
                                                # self.setGraphicsEffect(QGraphicsColorizeEffect())
                                                self.__hovered = True
                                                self.update()
                                                self.prepareGeometryChange()
                                        
                                            def hoverLeaveEvent(self, event):
                                                self.setGraphicsEffect(None)
                                        
                                        K 1 Reply Last reply
                                        0
                                        • U user4592357

                                          @kenchan
                                          no this is what i get:

                                          no hover:
                                          alt text

                                          after hover:
                                          alt text

                                          code:

                                              def paint(self, painter, option, widget):
                                                  pen = QPen(Qt.red)
                                                  if self.__hovered:
                                                      # what should go here?
                                                      self.__hovered = False
                                                      pen.setColor(Qt.white)
                                                      pen.setWidth(8)
                                                      painter.setPen(pen)
                                                      painter.drawLine(QPoint(0, 0), QPoint(100, 100))
                                                      painter.drawLine(QPoint(0, 100), QPoint(100, 0))
                                          
                                                  pen.setWidth(4)
                                                  painter.setPen(pen)
                                          
                                                  line = QLine(QPoint(0, 0), QPoint(100, 100))
                                                  painter.drawLine(line)
                                                  painter.drawLine(QPoint(0, 100), QPoint(100, 0))
                                          
                                              def boundingRect(self):
                                                  return QRectF(0, 0, 108, 108)
                                          
                                              def hoverEnterEvent(self, event):
                                                  # self.setGraphicsEffect(QGraphicsColorizeEffect())
                                                  self.__hovered = True
                                                  self.update()
                                                  self.prepareGeometryChange()
                                          
                                              def hoverLeaveEvent(self, event):
                                                  self.setGraphicsEffect(None)
                                          
                                          K Offline
                                          K Offline
                                          kenchan
                                          wrote on last edited by
                                          #20

                                          @user4592357
                                          Hmm so that red thing is your thin line version?

                                          U 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