Results 1 to 3 of 3

Thread: Can QGraphicsItems be layered between children of other QGraphicsItems?

  1. #1
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Can QGraphicsItems be layered between children of other QGraphicsItems?

    As default behavior of the QGraphicsItems "Z Values", independent Top-Level QGraphicsItems apparently can't be drawn between distinct "Z" layers of the nested QGrahpicsItem children of other Top-Level QGraphicsItems.

    The Qt 4.6.3 example below shows these QGraphicsItems:

    1. Top-Level QGraphicsRectItem .. Z = 1.0
    2. ... Child QGraphicsTextItem .. Z = 3.0
    3. Top-Level QGraphicsLineItem .. Z = 2.0

    Notice that the Line Item obscures the Text Item, even though the Text Item has a higher Z-Value (but does not have the same parent QGraphicsItem). [Qt 4.6.3, 12-2010].

    QUESTION: Are there any QGraphicsScene or QGraphicsItem configuration properties which can modify this behavior?

    EnigmaActual.gifEnigma3D.gif
    Images are also available here. See also screenshot of problem in our application (RiverWare).

    Qt Code:
    1. // File: ZProblem.cpp
    2. //
    3. // QGraphicsItems:
    4. // (1) Top-Level QGraphicsRectItem .. Z = 1.0
    5. // (2) ... Child QGraphicsTextItem .. Z = 3.0
    6. // (3) Top-Level QGraphicsLineItem .. Z = 2.0
    7. //
    8. // Notice that the Line Item obscures the Text Item, even though the
    9. // Text Item has a higher Z-Value (but does not have the same parent
    10. // QGraphicsItem). [Qt 4.6.3, 12-2010].
    11.  
    12. #include <QApplication>
    13. #include <QGraphicsScene>
    14. #include <QGraphicsView>
    15. #include <QGraphicsLineItem>
    16. #include <QGraphicsRectItem>
    17. #include <QGraphicsTextItem>
    18. #include <ostream>
    19.  
    20. int main(int argc, char** argv)
    21. {
    22. QApplication app (argc, argv);
    23. QGraphicsScene myScene;
    24.  
    25. // **************************************************
    26. // *** Create Top-Level Rectangle QGraphicsItem ***
    27. // **************************************************
    28.  
    29. const int RectWidth (300.0);
    30. const int RectHeight (220.0);
    31.  
    32. -0.5 * RectWidth, -0.5 * RectHeight, RectWidth, RectHeight);
    33.  
    34. rectItem->setBrush (QColor (0x66, 0xCC, 0xCC)); // blue-green
    35. rectItem->setPen (QColor (Qt::black));
    36. rectItem->setFlags (QGraphicsItem::ItemIsMovable);
    37.  
    38. rectItem->setZValue (1.0); // *** Z VALUE 1.0, Top-Level Item ***
    39. rectItem->setToolTip ("Rectangle: Z = 1.0");
    40.  
    41. myScene.addItem (rectItem);
    42.  
    43. // *****************************************
    44. // *** Create Child Text QGraphicsItem ***
    45. // *****************************************
    46.  
    47. QGraphicsTextItem* childTextItem =
    48. new QGraphicsTextItem ("ENIGMA", rectItem);
    49.  
    50. QFont textFont; // application default
    51. textFont.setPixelSize (75);
    52. textFont.setWeight (QFont::Black);
    53. childTextItem->setFont (textFont);
    54.  
    55. childTextItem->setZValue (3.0); // *** Z VALUE 3.0, Child Item ***
    56. childTextItem->setToolTip ("Child Text: Z = 3.0");
    57.  
    58. const QRectF childRect = childTextItem->boundingRect();
    59. childTextItem->setPos (-0.5 * childRect.width(),
    60. -0.5 * childRect.height());
    61.  
    62. // *********************************************
    63. // *** Create Top-Level Line QGraphicsItem ***
    64. // *********************************************
    65.  
    66. -0.6 * RectWidth, 0.0, 0.6 * RectWidth, 0.0);
    67.  
    68. QPen linePen = QPen (QColor (0xFF, 0x66, 0x66)); // red
    69. linePen.setCapStyle (Qt::RoundCap);
    70. linePen.setWidth (20);
    71. lineItem->setPen (linePen);
    72. lineItem->setFlags (QGraphicsItem::ItemIsMovable);
    73.  
    74. lineItem->setZValue (2.0); // *** Z VALUE 2.0, Top-Level Item ***
    75. lineItem->setToolTip ("Line: Z = 2.0");
    76.  
    77. myScene.addItem (lineItem);
    78.  
    79. // ******************************
    80. // *** Create QGraphicsView ***
    81. // ******************************
    82.  
    83. QGraphicsView myView (&myScene);
    84. myView.setCaption ("Test QGraphicsItems Z-Values");
    85. myView.resize (3.0 * RectWidth, 3.0 * RectHeight);
    86. myView.show();
    87.  
    88. return app.exec();
    89. }
    To copy to clipboard, switch view to plain text mode 

    Thank you in advance,
    Phil Weinstein, CADSWES
    University of Colorado at Boulder, USA

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: Can QGraphicsItems be layered between children of other QGraphicsItems?

    As far as I understand the QGraphicsItem documentation, what you want is not possible "out of the box".

    The Z value should be seen as a sorting value for the object tree.
    The sorting is limited to children.

    Example:
    Qt Code:
    1. parent 1 Z = 0
    2. child 1 Z = 1
    3. child 2 Z = 2
    4. child 2.1 Z = 3
    5. parent 2 Z = 4
    To copy to clipboard, switch view to plain text mode 

    If you change the Z value of parent 2 to 2 for example, it will not affect the order since the only item that is considered is parent 1 which has a Z = 0
    If you change child 1 to a Z value of 10, child 2 will be drawn before child 1, but parent 2 will still be drawn after the complete parent 1 is drawn.

    You can change this behaviour though by implementing your own stacking.

    There is a setting you can change though:
    QGraphicsItem::ItemStacksBehindParent
    But that doesn't do what you want either.

    Disclaimer: I might have interpretted the documentation wrong though
    http://doc.qt.nokia.com/4.7/qgraphicsitem.html#sorting
    Last edited by tbscope; 8th December 2010 at 10:45.

  3. The following user says thank you to tbscope for this useful post:

    Seishin (3rd January 2013)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Can QGraphicsItems be layered between children of other QGraphicsItems?

    There is a simple rule - the Z-order applies to items within the same parent (siblings). Period. It's strictly an extension (or realization) of the "painter's algorithm". You cannot change this behaviour in any way other than breaking your parent-child hierarchy or doing the whole GV event processing on your own. It's also natural to the logic of the graphics view architecture - the item and all its children are seen by the environment (parent, cousins) of the item as a single item. Hence you cannot stick something "inbetween" such a hierarchy because from the point of view of the architecture there is no "inbetween".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Selecting QGraphicsItems
    By SixDegrees in forum Qt Programming
    Replies: 3
    Last Post: 20th September 2010, 12:55
  2. qt 4.4 Issue with too many QGraphicsItems
    By spawn9997 in forum Qt Programming
    Replies: 6
    Last Post: 2nd September 2009, 15:56
  3. PopupMenu for several QGraphicsItems
    By jobrandt in forum Qt Programming
    Replies: 0
    Last Post: 10th August 2009, 15:00
  4. Problems with QGraphicsItems
    By JonathanForQT4 in forum Qt Programming
    Replies: 5
    Last Post: 26th April 2007, 08:25
  5. QGraphicsItems on top of each other?
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 18th November 2006, 20:23

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.