Unexpected behavior of the QStackedWidget when I insert twice or more the same QWidget (Wrong position of QWidget).
-
I hope this is the correct place to report a possible issue.
If in a QStackedWidget I insert more of once the same QWidget, all QWidgets are in the wrong position. (Please see image and application output)
Expected behavior is:
Pos pWidget1 0
Pos pWidget2 1
Pos pWidget3 2
Pos pWidget4 4m_pageStack->indexOf(pWidget1); I Expected the first position of item, in my case 0.
I reproduce this behavior in Windows and Linux, Qt4 and Qt5.
Best regards
-
@CP71 said in Unexpected behavior of the QStackedWidget when I insert twice or more the same QWidget (Wrong position of QWidget).:
I Expected the first position of item, in my case 0.
There is no first and second. When you call
addWidget
the widget gets taken out of wherever it was parented to and moved as a child of the stacked page. when you calladdWidget(pWidget1)
the second time you are moving the widget from pos 0 to pos 2 -
@CP71 said:
If in a QStackedWidget I insert more of once the same QWidget, all QWidgets are in the wrong position.
A given widget object can appear only once in the stack. If you insert the same Widget1 object twice then it moves from where it was after to the first insert to where you put it the second time. This causes stack indices to change (there are no index gaps like your expectation). This what your code does.
If you insert two separate instances of a Widget1 widget then you will get two visually identical but independent pages in the stack. Doing this would give you five widgets in the stack: Widget1, Widget2, Widget3, Widget1, Widget4.
If you want a totally blank page in the stack (cannot think why) then just insert a bare QWidget object.
-
@ChrisW67
Thanks to all.
I don't have a qwidget twice in the QStackedWidget,
I was porting a Linux application to Windows when for compile issue in a UI I temporarily switched it with one that already exists because I have some defines with indexes of pages.
When I have inserted twice the same qwidget all my defines were not related to the real position of widgets, up at once it seemed strange to me.
Yes, I did as you say, I have temporarily inserted an empty QWidget.