PDA

View Full Version : QObject children shuffle



ugluk
13th August 2012, 07:31
if you check out this QObject's method:



const QObjectList & children () const


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?

yeye_olive
13th August 2012, 09:07
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?

ugluk
13th August 2012, 09:15
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

yeye_olive
13th August 2012, 10:01
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.


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.

StrikeByte
13th August 2012, 10:57
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

ugluk
13th August 2012, 11:25
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:


tab
-> tab item
-> tab item
...

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.

yeye_olive
13th August 2012, 12:45
I need to keep 2 trees in sync. One is an element specification tree, say:


tab
-> tab item
-> tab item
...

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?

ugluk
13th August 2012, 15:17
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.