Results 1 to 5 of 5

Thread: Hiding children of QGraphicsItem

  1. #1
    Join Date
    Jul 2016
    Posts
    19
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Hiding children of QGraphicsItem

    Hello,

    I have a QGraphicsItem, called myitem, which has several children. When myitem is selected I want to display its children and when it is not selected, I hide them. While the children weren't selectable, it worked perfectly with something like that :

    Qt Code:
    1. QVariant QGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value)
    2. {
    3. if(change == QGraphicsItem::ItemSelectedChange)
    4. {
    5. if(value.toBool())
    6. {
    7. //item is selected
    8. showChildren(true);
    9. }else
    10. {
    11. //item is deselected
    12. showChildren(false);
    13.  
    14. }
    15. }
    16. ...
    17. }
    To copy to clipboard, switch view to plain text mode 

    But now the children of myitem are selectable and the above code doesn't work anymore because if I deselect myitem to select one of its children I hide it. In this situation, I shouldn't hide the children of myitem.
    So I have to know the new selected item in QGraphicItem::itemChange to correctly show/hide the children. How can I do that ?
    In change == QGraphicsItem::ItemSelectedChange or change == QGraphicsItem::ItemSelectedhasChanged, scene()->selectedItems() doesn't return the newly selected items.


    Thank you
    Last edited by nilot; 30th May 2017 at 12:06.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Hiding children of QGraphicsItem

    connect to
    Qt Code:
    1. [signal] void QGraphicsScene::focusItemChanged(QGraphicsItem *newFocusItem, QGraphicsItem *oldFocusItem, Qt::FocusReason reason)
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jul 2016
    Posts
    19
    Thanks
    9
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Hiding children of QGraphicsItem

    Are you sure that focus and selection are the same thing ? Only one item can have focus but more than one qgraphicsitem can be selected at the same time.

    Suppose that two items are selected then unselected, do your signal will be trigger two times ?

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Hiding children of QGraphicsItem

    Focus and selection are different, this signal will be emitted only once, even if multiple items are selected. You will need to check the Item selection when focus changes.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    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: Hiding children of QGraphicsItem

    It seems easiest to just check in itemChange() whether any of the children are selected. If so, don't hide them.
    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. QGraphicsItem::ItemIgnoresTransformations and children
    By pmjobin in forum Qt Programming
    Replies: 7
    Last Post: 10th December 2014, 18:53
  2. Replies: 2
    Last Post: 9th September 2011, 09:57
  3. Hiding QGraphicsRectItems
    By ddze in forum Qt Programming
    Replies: 4
    Last Post: 29th January 2011, 10:01
  4. Replies: 7
    Last Post: 29th November 2010, 19:20
  5. Replies: 2
    Last Post: 6th February 2010, 16:31

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.