Results 1 to 8 of 8

Thread: QGraphicsText parent item on top of QGraphicsRectItem

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QGraphicsText parent item on top of QGraphicsRectItem

    Hi,

    I have a QGraphicsScene with a QGraphicsTextItem and a QGraphicsRectItem as child of the text item.

    What I want is the text item to be displayed on top of the rect item and move the rect item when the text is moved.

    The problem is that the rect item is always displayed on top of the text item. The only way to display the text on top of the rect item is to not set the text item as parent of the text rect(then the two items are independents and this is not what I want).

    Am I missing something?

    Qt Code:
    1. QGraphicsScene *pqScene = new QGraphicsScene(this);
    2. ui.graphicsView->setScene(pqScene);
    3.  
    4. pqText->setPlainText("Hello");
    5. //QGraphicsTextItem *pqText = pqScene->addText("Hello"); //Tried this with same result
    6. pqText->setZValue(0);
    7. pqText->setFlags(QGraphicsItem::GraphicsItemFlag::ItemIsMovable);
    8.  
    9. QGraphicsRectItem *pqRect = new QGraphicsRectItem(pqText);
    10. //QGraphicsRectItem *pqRect = pqScene->addRect(pqText->boundingRect()); //Tried this with same result
    11. //pqRect->setParentItem(pqText);
    12. pqRect->setBrush(Qt::red);
    13. pqRect->setZValue(-1);
    14. pqRect->setRect(pqText->boundingRect());
    15.  
    16. pqScene->addItem(pqText);
    17. pqScene->addItem(pqRect);
    To copy to clipboard, switch view to plain text mode 

    Using Qt 5.4.1 on Windows with Visual Studio 2013.

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsText parent item on top of QGraphicsRectItem

    Maybe put both as sibling into the same parent item?
    E.g. a QGraphicsItemGroup?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsText parent item on top of QGraphicsRectItem

    Hi,

    I created a QGraphicsTextItem derivred class that has a QGraphicsRectItem as child. When the text item is modified(font, text,...) the child rect item modifies its rect to the text bounding rectangle.
    Is this not a good approach? So, what is the parent-child relationship used for? I thought it was for something like this.

    Another solution could be to use the paint method of the text derived class to paint the bounding rect and then the text, but by the moment I want to understand what I'm doing wrong.

    Thanks,
    Òscar Llarch i Galán

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsText parent item on top of QGraphicsRectItem

    Quote Originally Posted by ^NyAw^ View Post
    I created a QGraphicsTextItem derivred class that has a QGraphicsRectItem as child. When the text item is modified(font, text,...) the child rect item modifies its rect to the text bounding rectangle.
    Is this not a good approach?
    If it does what you need then it is a good approach.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsText parent item on top of QGraphicsRectItem

    Hi,

    If it does what you need then it is a good approach.
    No, it doesn't.

    My example code shows that the rect item is ALWAYS displayed on top of the text item(the text is not visible) and I'm not able to display the text item on top of the rect item.

    This is what I get
    image1.png

    This is what I want
    image2.png
    Òscar Llarch i Galán

  6. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsText parent item on top of QGraphicsRectItem

    So,

    Am I doing it wrong or is a Qt bug?
    Òscar Llarch i Galán

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsText parent item on top of QGraphicsRectItem

    Quote Originally Posted by ^NyAw^ View Post
    Am I doing it wrong or is a Qt bug?
    Can easily be answered by looking at the code :-)

    In this case even by looking into the documentation:
    Quote Originally Posted by The GraphicsItem documentation
    You can set the ItemStacksBehindParent flag to stack a child item behind its parent.
    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    ^NyAw^ (25th February 2016)

  9. #8
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsText parent item on top of QGraphicsRectItem

    Hi,


    In this case even by looking into the documentation:
    Quote Originally Posted by The GraphicsItem documentation
    You can set the ItemStacksBehindParent flag to stack a child item behind its parent.
    This works perfectly. I thought that changing the z value of the item will do the same.
    Òscar Llarch i Galán

Similar Threads

  1. how to ignore parent qml item scale?
    By waiter in forum Qt Quick
    Replies: 0
    Last Post: 10th July 2013, 11:03
  2. QGraphicsText Item Rotation and antialiasing
    By yogeshupreti in forum Qt Programming
    Replies: 4
    Last Post: 23rd December 2012, 03:20
  3. Can QGraphicsItem's parent item be from another scene?
    By cresta in forum Qt Programming
    Replies: 3
    Last Post: 25th September 2012, 21:51
  4. Moving parent item
    By barisatamer in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2011, 10:49
  5. How to get parent of selected item in TreeView?
    By RavenS in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2009, 14:27

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.