Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-81316

QGraphicsItem::setZValue not working

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P2: Important
    • Resolution: Won't Do
    • Affects Version/s: 5.14.0 RC2
    • Fix Version/s: None
    • Component/s: Widgets: GraphicsView
    • Labels:
      None
    • Platform/s:
      Windows

      Description

      This is a very recent issue, and I wasn't having this issue at all before, so I'm assuming it's strictly relevant to a recent version.

      In short, QGraphicsItem::setZValue does practically nothing.

      I have to reorder how I initialize my graphics items. This however, won't be enough to solve every issue efficiently. 

      Here's a basic amount of code:

      .h file:

      class GraphicsView : public QGraphicsView
      {
          QGraphicsRectItem *SelectionRect; QGraphicsItemGroup *SelectionGroup;
          QGraphicsRectItem *SheetRect; 
          void initialize();
      
      public:
          GraphicsView(QWidget *parent = nullptr);
          GraphicsView(QGraphicsScene *scene, QWidget *parent = nullptr);
          ~GraphicsView();
      }

      .cpp file (inherits QGraphicsView):

      GraphicsView::GraphicsView(QWidget *parent) : QGraphicsView(parent) 
      { setScene(new QGraphicsScene); initialize(); }
      
      GraphicsView::GraphicsView(QGraphicsScene *scene, QWidget *parent) : QGraphicsView(scene,parent)
      { initialize(); }
      
      void GraphicsView::initialize() { 
          setViewportUpdateMode(ViewportUpdateMode::FullViewportUpdate);        
          setSceneRect(QRectF(0,0,200,400));
      
          setBackgroundBrush(Qt::Dense6Pattern);
      
          SheetRect = new QGraphicsRectItem(scene()->addRect(sceneRect(),QPen(),QBrush(Qt::white))); 
          SheetRect->setZValue(-std::numeric_limits<qreal>::max()); 
      
          SelectionRect = new QGraphicsRectItem(scene()->addRect(QRectF())); 
          SelectionRect->setZValue(std::numeric_limits<qreal>::max()); 
          SelectionRect->setPen(QPen(Qt::DashLine)); setMouseTracking(true);
      }
      
      GraphicsView::~GraphicsView()
      {
          delete SelectionRect;
          delete SheetRect;
      }
      

      The above code keeps everything in proper order. 

      However, if I reorder how I initialize things, then the appearance is entirely different, in spite of the fact that I've defined their zValues. That is, whatever is defined first, shows at the back, and whatever is defined last, shows up at the front, regardless of the z-value set.

      Even if It set the background brush after initializing the SheetRect, the SheetRect will move behind the the Dense6Pattern. If the SelectionRect is initialized before the SheetRect, then the SelectionRect moves behind the SheetRect.

      Again, this wasn't always the case. Setting the z-value did the trick for a while. I'd just prefer the issue be "refixed".

       

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            • Assignee:
              bibr Andreas Aardal Hanssen
              Reporter:
              osamakawish Osama Kawish
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Gerrit Reviews

                There are no open Gerrit changes