Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[SOLVED] QGraphicsScene Layer



  • I want to draw Circles with various depths.

    Like this:
    http://www.informatik.uni-koeln.de/ls_juenger/fileadmin/projects/geodual/sc04.png

    But it happens that they disappear or some seem to not appear at all.

    I am using addEllipse() for this.

    Does someone know how to handle the layers of a QGraphicsscene properly?


  • Lifetime Qt Champion

    Hi,

    Isn't setZValue what you are looking for ?



  • @SGaist

    yep, now i initialized all GraphicItems by using GraphicsEllipseItem instead of adding them directly.
    But now i got following problem:
    The first layer got zValue 0
    The second -1
    .....
    But somehow my elements on depth -3 wont get painted.
    Is there a limit or some sort of event when the GraphicsScene clears itself?

    Here is a code snippet:

     if(y!=0){
                   QGraphicsEllipseItem* el = new QGraphicsEllipseItem();
                   QRect rect4(v_to_paint->pos.x(),v_to_paint->pos.y(),(v_to_paint->yv+y)*2,(v_to_paint->yv+y)*2);
                   el->setRect(rect4);
                   el->setPen(Pen);
                   el->setBrush(Brush);
                   el->setZValue(depth);
                   painter->addItem(el);
               }
    
               int type = v_to_paint->type;
    
    
               if((type==1||type==2)&&(v_to_paint->isRoot==false)){
               QGraphicsEllipseItem* el1 = new QGraphicsEllipseItem();
               QRect rect(v_to_paint->pos.x()-v_to_paint->yv,v_to_paint->pos.y()-v_to_paint->yv,v_to_paint->yv*2,v_to_paint->yv*2);
               el1->setRect(rect);
               el1->setPen(greenPen);
               el1->setBrush(greenBrush);
               el1->setZValue(-2);
               painter->addItem(el1);
    
               QGraphicsEllipseItem* el2 = new QGraphicsEllipseItem();
               QRect rect2(v_to_paint->pos.x()-3,v_to_paint->pos.y()-3,6,6);
               el2->setRect(rect2);
               el2->setPen(blackPen);
               el2->setBrush(blackBrush);
               el2->setZValue(-1);
               painter->addItem(el2);
    

    The first one "el" is the one that wont get painted.


  • Lifetime Qt Champion

    You should take a look at the Drag and Drop Robot example, the use of setZValue is shown there


Log in to reply