Results 1 to 8 of 8

Thread: QObject children shuffle

  1. #1
    Join Date
    Jun 2011
    Posts
    56
    Qt products
    Qt4
    Thanks
    7

    Default QObject children shuffle

    if you check out this QObject's method:

    Qt Code:
    1. const QObjectList & children () const
    To copy to clipboard, switch view to plain text mode 

    you can see, that the list can be const_cast-ed and then changed (e.g. the children can be shuffled around). Can this lead to trouble?

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: QObject children shuffle

    Although it might work (I have not looked at the sources to know for sure), you would be asking for trouble. Besides, even if it works with a particular version of Qt on a particular system configuration, there is no guarantee that it will always be the case. Also, some operations like adding or removing children are necessarily more complex that just adding or removing pointers to/from this list. If we were supposed to manipulate this list, don't you think there would be a non-const version of children()? There are lots of ways you can abuse C/C++; const_casting is one of them.

    Out of curiosity, why would you need to control the order of the elements of that list?

  3. #3
    Join Date
    Jun 2011
    Posts
    56
    Qt products
    Qt4
    Thanks
    7

    Default Re: QObject children shuffle

    I'm writing a form editor and need to keep 2 trees in sync. Interestingly, I don't know of a way to shuffle children around in Qt Designer. Also, I think your statement:
    Besides, even if it works with a particular version of Qt on a particular system configuration, there is no guarantee that it will always be the case.
    is not entirely accurate. There are some guarantees given by the C++ standards. The system configurations might differ, but the qobject.cpp code stays the same. I don't see any platform-specific code in there. Further, I regularly see const_casts in Qt source code (quick grep reveals):
    find -type f | xargs grep const_cast | wc -l
    1249

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: QObject children shuffle

    Quote Originally Posted by ugluk View Post
    I'm writing a form editor and need to keep 2 trees in sync. Interestingly, I don't know of a way to shuffle children around in Qt Designer.
    What do you mean? Does your editor GUI contain two tree widgets and you need a way to reorder the children of a given node? QObject::children() would not help you if this was what you were trying to achieve.

    Quote Originally Posted by ugluk View Post
    The system configurations might differ, but the qobject.cpp code stays the same. I don't see any platform-specific code in there. Further, I regularly see const_casts in Qt source code (quick grep reveals):[...]
    Even if qobject.cpp does not use any platform-specific code and does not call any function containing platform-specific code relevant to the point under discussion and therefore behaves the same across all configurations, how do you know that the behaviour will remain the same in future versions of Qt? The fact that Qt makes use of const_cast internally does not mean that you should do the same. My point is not that const_cast is always bad, but that you should not use it to get around a safeguard. Clearly you are not supposed to modify the list QObject::children(); const_cast technically allows you to do so, but you have no control over the consequences of doing so.

  5. #5
    Join Date
    May 2012
    Posts
    136
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 27 Times in 24 Posts

    Default Re: QObject children shuffle

    Why do you want to write a new gui editor, you can use designer (change it if you need) and load the ui at runtime

  6. #6
    Join Date
    Jun 2011
    Posts
    56
    Qt products
    Qt4
    Thanks
    7

    Default Re: QObject children shuffle

    What do you mean? Does your editor GUI contain two tree widgets and you need a way to reorder the children of a given node? QObject::children() would not help you if this was what you were trying to achieve.
    I need to keep 2 trees in sync. One is an element specification tree, say:
    Qt Code:
    1. tab
    2. -> tab item
    3. -> tab item
    4. ...
    To copy to clipboard, switch view to plain text mode 
    The other tree is the actual widget tree. If the order of elements in the element specification tree changes, so must the order in the widget tree. I'm not trying to re-implement designer. My GUI editor is a lot simpler. By 'tree' I mean a data structure.

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanked 97 Times in 94 Posts

    Default Re: QObject children shuffle

    Quote Originally Posted by ugluk View Post
    I need to keep 2 trees in sync. One is an element specification tree, say:
    Qt Code:
    1. tab
    2. -> tab item
    3. -> tab item
    4. ...
    To copy to clipboard, switch view to plain text mode 
    The other tree is the actual widget tree. If the order of elements in the element specification tree changes, so must the order in the widget tree. I'm not trying to re-implement designer. My GUI editor is a lot simpler. By 'tree' I mean a data structure.
    I can only see two ways for changing the order of the children of a QObject:
    - for QWidgets, use QWidget::lower() or QWidget::raise();
    - for arbitrary QObjects, de-parent and re-parent the child.
    You can perform any permutation of the children by doing many such operations in sequence.

    Do you need to specify the order of the children because it is also the Z-order?

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

    ugluk (13th August 2012)

  9. #8
    Join Date
    Jun 2011
    Posts
    56
    Qt products
    Qt4
    Thanks
    7

    Default Re: QObject children shuffle

    Do you need to specify the order of the children because it is also the Z-order?
    Yes, the QGraphicsItem s have this solved differently, but I'm stuck with widgets, as there are practically no QGraphicsItem controls implemented and implementing them with QML is generally not-so-easy as advertised. Thanks for the lower/raise idea, I'll look into it.

Similar Threads

  1. Only get children
    By wirasto in forum Newbie
    Replies: 1
    Last Post: 24th January 2010, 17:15
  2. Replies: 4
    Last Post: 11th February 2009, 18:21
  3. Replies: 2
    Last Post: 20th September 2007, 19:37
  4. children()
    By vermarajeev in forum Qt Programming
    Replies: 2
    Last Post: 14th May 2007, 15:15
  5. Replies: 2
    Last Post: 6th December 2006, 23:41

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
  •  
Qt is a trademark of The Qt Company.