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. QGraphicsItems ZValue and detection priority
Forum Updated to NodeBB v4.3 + New Features

QGraphicsItems ZValue and detection priority

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 3.6k Views 1 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.
  • GofferG Offline
    GofferG Offline
    Goffer
    wrote on last edited by
    #1

    Hey guys,

    I have 2 QGraphicsItems,
    1 with a rect (0, 0, 200, 50) (this one has nothing painted in it)
    2nd one parented under the first one with a rect (0, 0, 200, 200) (this one has a background)

    I am setting the 2nd with a ZValue of -1 so it's displayed behind the parent.

    When I try to find the item at a click position, it always detects the 2nd one that is supposed to be behind the first one. How is that possible ??? the one with the highest ZValue should be detected first.

    Any idea ?

    Thx

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kenchan
      wrote on last edited by
      #2

      Hi @Goffer
      Since the default z value for a graphics item os 0 perhaps -1 is not a good value for this?? I never use -1 for this always greater than 0.
      If you have made your own subclass of graphics items make sure your bounding box return value is correct.
      I assume you have made the item selectable right?
      Other than that I am not sure what the problem might be.

      1 Reply Last reply
      0
      • GofferG Offline
        GofferG Offline
        Goffer
        wrote on last edited by Goffer
        #3

        Hey @kenchan

        I ve tried 0 and 1 too but it's the same.
        Actually the main item is selectable and the child is not (since that's the behavior I need) but it always returns the child that is behind ...

        EDIT
        So, somehow the child item doesn't go behind even by setting the ZValue, here is a bit of code that represents what I am trying to do.

        from PySide2 import QtWidgets, QtGui, QtCore
        
        class View(QtWidgets.QGraphicsView):
        
            def __init__(self, parent):
                super(View, self).__init__(parent)
        
            def mousePressEvent(self, event):
        
                itemAtPos = self.scene().itemAt(self.mapToScene(event.pos()), QtGui.QTransform())
                print itemAtPos
        
                super(View, self).mousePressEvent(event)
        
        class Item1(QtWidgets.QGraphicsItem):
        
            def __init__(self, parent):
                super(Item1, self).__init__(parent)
        
                self.setAcceptHoverEvents(True)
                self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable)
                self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
        
            def boundingRect(self):
                """
                The bounding rect based on the width and height variables.
        
                """
                rect = QtCore.QRectF(QtCore.QRect(0, 0, 200, 50))
                return rect
        
            def shape(self):
                """
                The shape of the Slot is a circle.
        
                """
                path = QtGui.QPainterPath()
                path.addRect(self.boundingRect())
                return path
        
            def paint(self, painter, option, widget):
                """
                Paint the item.
        
                """
                self._brush = QtGui.QBrush()
                self._brush.setStyle(QtCore.Qt.SolidPattern)
                self._brush.setColor(QtGui.QColor(200,200,200,255))
               
                painter.setBrush(self._brush)
                painter.drawRect(self.boundingRect())
        
        
        class Item2(QtWidgets.QGraphicsItem):
        
            def __init__(self, parent):
                super(Item2, self).__init__(parent)
        
            def boundingRect(self):
                """
                The bounding rect based on the width and height variables.
        
                """
                rect = QtCore.QRectF(QtCore.QRect(0, 0, 200, 500))
                return rect
        
            def shape(self):
                """
                The shape of the Slot is a circle.
        
                """
                path = QtGui.QPainterPath()
                path.addRect(self.boundingRect())
                return path
        
            def paint(self, painter, option, widget):
                """
                Paint the item.
        
                """
                self._brush = QtGui.QBrush()
                self._brush.setStyle(QtCore.Qt.SolidPattern)
                self._brush.setColor(QtGui.QColor(200,0,0,255))
               
                painter.setBrush(self._brush)
                painter.drawRect(self.boundingRect())
        
        
        v = View(parent=None)
        s = QtWidgets.QGraphicsScene()
        v.setScene(s)
        
        i1 = Item1(parent=None)
        i1.setZValue(1)
        
        i2 = Item2(parent=i1)
        i2.setZValue(0)
        
        s.addItem(i1)
        
        v.show()
        
        
        1 Reply Last reply
        0
        • K Offline
          K Offline
          kenchan
          wrote on last edited by
          #4

          I don't use PySide so I cannot offer much help on this one.
          I have possibly dumb question though. Don't you need to add i2 to the scene as wall as i1, like s.addItem(i2)??
          If this was c++ you would have to do that.

          1 Reply Last reply
          0
          • GofferG Offline
            GofferG Offline
            Goffer
            wrote on last edited by
            #5

            Nope,
            since you specify it's a child of i1 and i1 is already in the scene.
            And it's being displayed in the view so it should be fine (If i had to add it , I wouldn't see it in the view). I've never had to add my children items to the scene.

            (I've tried to add it just in case but it doesn't change anything)
            Could you try to do that in C++ and see if it works ?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kenchan
              wrote on last edited by kenchan
              #6

              @Goffer
              I tried it in c++ and I got similar behaviour until i set the QGraphicsItem::ItemStacksBehindParent flag to true on item 2. Then i was able to select either item 1 or item 2 by clicking on each item.

              1 Reply Last reply
              0
              • GofferG Offline
                GofferG Offline
                Goffer
                wrote on last edited by
                #7

                @kenchan said in QGraphicsItems ZValue and detection priority:

                ItemStacksBehindParent

                Ohhhhh, I didn't know about that one !! But that's a bit confusing that it's not possible just by using the Zvalue as it seems to be designed for that purpose ...

                Anyway, I gave it a try and it works.

                Thank you !

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kenchan
                  wrote on last edited by
                  #8

                  Yes, I am not sure what is going on with the zvalue in this case.
                  Glad it to hear it works for you now though.

                  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