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

highlight QGraphicsItem

Scheduled Pinned Locked Moved Solved General and Desktop
43 Posts 4 Posters 13.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.
  • U user4592357
    18 Feb 2018, 11:41

    @kenchan
    thnaks.

    i was missing the call to prepareGeometryChange() in hoverLeaveEvent

    by the way i noticed that when i hover over the bottom (in picture) item, it "goes up". when i hover over the top item, it "goes down".

    this happens only after first time hovering. after that they don't move.

    what is the reason?

    K Offline
    K Offline
    kenchan
    wrote on 18 Feb 2018, 12:08 last edited by
    #32

    @user4592357
    that is probably the scene or the view adjusting the scaling for the added items. That is why I asked about the settings you are applying to those before doing the drawing.

    U 1 Reply Last reply 18 Feb 2018, 14:02
    0
    • U user4592357
      18 Feb 2018, 11:41

      @kenchan
      thnaks.

      i was missing the call to prepareGeometryChange() in hoverLeaveEvent

      by the way i noticed that when i hover over the bottom (in picture) item, it "goes up". when i hover over the top item, it "goes down".

      this happens only after first time hovering. after that they don't move.

      what is the reason?

      K Offline
      K Offline
      kenchan
      wrote on 18 Feb 2018, 12:17 last edited by
      #33

      @user4592357
      Please mark the thread as solved if that is the case.

      1 Reply Last reply
      0
      • K kenchan
        18 Feb 2018, 12:08

        @user4592357
        that is probably the scene or the view adjusting the scaling for the added items. That is why I asked about the settings you are applying to those before doing the drawing.

        U Offline
        U Offline
        user4592357
        wrote on 18 Feb 2018, 14:02 last edited by user4592357
        #34

        @kenchan
        the only setting i have is applied to the view:

        setRenderHint(QPainter.Antialiasing)
        setRenderHint(QPainter.TextAntialiasing)
        

        removing them doesn't solve the problem, however.

        K 1 Reply Last reply 18 Feb 2018, 14:16
        0
        • U user4592357
          18 Feb 2018, 14:02

          @kenchan
          the only setting i have is applied to the view:

          setRenderHint(QPainter.Antialiasing)
          setRenderHint(QPainter.TextAntialiasing)
          

          removing them doesn't solve the problem, however.

          K Offline
          K Offline
          kenchan
          wrote on 18 Feb 2018, 14:16 last edited by
          #35

          @user4592357
          No they wouldn't, i was thinking more in terms of...

          setViewportUpdateMode()
          setResizeAnchor()
          setDragMode()
          setHorizontalScrollBarPolicy()
          setSceneRect()
          

          and the like on the QGraphicsView. These things can affect how the scene behaves. It is a good idea to check how they affect yours.

          U 1 Reply Last reply 18 Feb 2018, 14:30
          0
          • K kenchan
            18 Feb 2018, 14:16

            @user4592357
            No they wouldn't, i was thinking more in terms of...

            setViewportUpdateMode()
            setResizeAnchor()
            setDragMode()
            setHorizontalScrollBarPolicy()
            setSceneRect()
            

            and the like on the QGraphicsView. These things can affect how the scene behaves. It is a good idea to check how they affect yours.

            U Offline
            U Offline
            user4592357
            wrote on 18 Feb 2018, 14:30 last edited by
            #36

            @kenchan
            setSceneRect() seems to do the job. but how do i tell it to use all the available area as scene rect? because i might have dockable widgets enabled in which case i need the scene rect to be smaller (i hope i could explain it)

            K 1 Reply Last reply 18 Feb 2018, 14:42
            0
            • U user4592357
              18 Feb 2018, 14:30

              @kenchan
              setSceneRect() seems to do the job. but how do i tell it to use all the available area as scene rect? because i might have dockable widgets enabled in which case i need the scene rect to be smaller (i hope i could explain it)

              K Offline
              K Offline
              kenchan
              wrote on 18 Feb 2018, 14:42 last edited by kenchan
              #37

              @user4592357
              Yes well that is the tricky bit. the sceneRect is the part of the scene you want to fit into the viewport rectangle.
              make it bigger in scene space and it looks smaller in the viewport and so on. You can make it scale automatically to the items in the scene or do it manually.

              U 1 Reply Last reply 18 Feb 2018, 16:48
              2
              • K kenchan
                18 Feb 2018, 14:42

                @user4592357
                Yes well that is the tricky bit. the sceneRect is the part of the scene you want to fit into the viewport rectangle.
                make it bigger in scene space and it looks smaller in the viewport and so on. You can make it scale automatically to the items in the scene or do it manually.

                U Offline
                U Offline
                user4592357
                wrote on 18 Feb 2018, 16:48 last edited by
                #38

                @kenchan
                this did the trick ():

                view->setSceneRect(scene()->sceneRect())
                

                and my last question. as you could see in the above pictures, the crosses' bounding rects may overlap. in that case, when, for example cursor is in bottom cross's bounding rect, and i try to touch the lower right part of above cross, the highlight for the above cross isn't displayed.

                is this issue solvable?

                K 1 Reply Last reply 19 Feb 2018, 00:03
                0
                • U user4592357
                  18 Feb 2018, 16:48

                  @kenchan
                  this did the trick ():

                  view->setSceneRect(scene()->sceneRect())
                  

                  and my last question. as you could see in the above pictures, the crosses' bounding rects may overlap. in that case, when, for example cursor is in bottom cross's bounding rect, and i try to touch the lower right part of above cross, the highlight for the above cross isn't displayed.

                  is this issue solvable?

                  K Offline
                  K Offline
                  kenchan
                  wrote on 19 Feb 2018, 00:03 last edited by kenchan
                  #39

                  @user4592357
                  Ok, now things might get a bit more complex. The selection is currently using the bounding box so when they overlap you need to do more work if you want to distinguish exactly which cross was hit. In this case the zValue of the items is making the one you drew last get the hit first? You can set the zValue when this is important for picking but I don't think it helps in this case :-).
                  Please read the documentation about the QGraphicsItem (the c++ docs I guess). If you implement the shape() method you can use a QPainterPath to return a more accurate shape, in your case lines with a thickness. You need to make it look at the thin version of the line because that is what the user will be seeing and trying to pick. You can use contains() and collidesWithPath() etc. to get a more precise picking experience :-). You will probably want to use these different testing functions in the event before you decide that the cursor is actually hovering over your shape. This kind of testing can get slow for complex shapes and when you have many shapes to test, but the bounding box test filters out things pretty quickly.
                  In the last resort you can implement you own geometric testing algorithms but you probably don't need to go that far, do you?

                  I hope this all helps :-)

                  U 1 Reply Last reply 19 Feb 2018, 16:11
                  2
                  • K kenchan
                    19 Feb 2018, 00:03

                    @user4592357
                    Ok, now things might get a bit more complex. The selection is currently using the bounding box so when they overlap you need to do more work if you want to distinguish exactly which cross was hit. In this case the zValue of the items is making the one you drew last get the hit first? You can set the zValue when this is important for picking but I don't think it helps in this case :-).
                    Please read the documentation about the QGraphicsItem (the c++ docs I guess). If you implement the shape() method you can use a QPainterPath to return a more accurate shape, in your case lines with a thickness. You need to make it look at the thin version of the line because that is what the user will be seeing and trying to pick. You can use contains() and collidesWithPath() etc. to get a more precise picking experience :-). You will probably want to use these different testing functions in the event before you decide that the cursor is actually hovering over your shape. This kind of testing can get slow for complex shapes and when you have many shapes to test, but the bounding box test filters out things pretty quickly.
                    In the last resort you can implement you own geometric testing algorithms but you probably don't need to go that far, do you?

                    I hope this all helps :-)

                    U Offline
                    U Offline
                    user4592357
                    wrote on 19 Feb 2018, 16:11 last edited by user4592357
                    #40

                    @kenchan
                    something like this?

                        def shape():
                            path = QPainterPath(QPointF(x(), y()))
                            path.setFillRule(Qt.WindingFill)
                            update()
                            path.lineTo(QPointF(100, 100))
                            return path
                    

                    this doesn't work though. i don't know how to return shape for two lines.

                    EDIT: what about using two QGraphicsLineItems? how would i return the shape() of the total then?

                    K 1 Reply Last reply 19 Feb 2018, 23:38
                    0
                    • U user4592357
                      19 Feb 2018, 16:11

                      @kenchan
                      something like this?

                          def shape():
                              path = QPainterPath(QPointF(x(), y()))
                              path.setFillRule(Qt.WindingFill)
                              update()
                              path.lineTo(QPointF(100, 100))
                              return path
                      

                      this doesn't work though. i don't know how to return shape for two lines.

                      EDIT: what about using two QGraphicsLineItems? how would i return the shape() of the total then?

                      K Offline
                      K Offline
                      kenchan
                      wrote on 19 Feb 2018, 23:38 last edited by kenchan
                      #41

                      @user4592357
                      You should draw a path which is exactly the same shape as your two lines. Using the same points and using the painter moveto or lineto functions.

                      QPainterPath path;
                      path.moveTo(0,0);
                      path.lineTo(100,100);
                      path.moveTo(0,100);
                      path.lineTo(100,0);
                      return path;
                      

                      if you want to include the thickness of the pen you use a QPainterPathStroker like this

                      QPainterPath path;
                      path.moveTo(0,0);
                      path.lineTo(100,100);
                      path.moveTo(0,100);
                      path.lineTo(100,0);
                      QPen pen(QBrush(QColor(255,0,0)),2,Qt::SolidLine,Qt::RoundCap);
                      QPainterPathStroker stroker(pen);
                      return stroker.createStroke(path);
                      

                      I have edited my code in the previous post and added the code for the shape. It works perfectly for me :-)

                      U 1 Reply Last reply 19 Feb 2018, 23:48
                      2
                      • K kenchan
                        19 Feb 2018, 23:38

                        @user4592357
                        You should draw a path which is exactly the same shape as your two lines. Using the same points and using the painter moveto or lineto functions.

                        QPainterPath path;
                        path.moveTo(0,0);
                        path.lineTo(100,100);
                        path.moveTo(0,100);
                        path.lineTo(100,0);
                        return path;
                        

                        if you want to include the thickness of the pen you use a QPainterPathStroker like this

                        QPainterPath path;
                        path.moveTo(0,0);
                        path.lineTo(100,100);
                        path.moveTo(0,100);
                        path.lineTo(100,0);
                        QPen pen(QBrush(QColor(255,0,0)),2,Qt::SolidLine,Qt::RoundCap);
                        QPainterPathStroker stroker(pen);
                        return stroker.createStroke(path);
                        

                        I have edited my code in the previous post and added the code for the shape. It works perfectly for me :-)

                        U Offline
                        U Offline
                        user4592357
                        wrote on 19 Feb 2018, 23:48 last edited by user4592357
                        #42

                        @kenchan
                        thank you so so much, works perfectly! i wish i was confident (or smart) enough to write that code myself...

                        K 1 Reply Last reply 19 Feb 2018, 23:53
                        0
                        • U user4592357
                          19 Feb 2018, 23:48

                          @kenchan
                          thank you so so much, works perfectly! i wish i was confident (or smart) enough to write that code myself...

                          K Offline
                          K Offline
                          kenchan
                          wrote on 19 Feb 2018, 23:53 last edited by kenchan
                          #43

                          @user4592357
                          Glad to hear it worked for you :-)
                          As you can see in this case just adding the shape function made the hover events respond to the more precise shape without the need for more checking code.
                          Is that cool or what!

                          1 Reply Last reply
                          1

                          41/43

                          19 Feb 2018, 23:38

                          • Login

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