QStackedWidget: Bind to combo box?
-
Hello,
Is it possible to "bind" (sorry for the .net term), the list of available widgets in a QStackedWidget to a combo box?
The "docs":http://doc.qt.nokia.com/latest/qstackedwidget.html show using a combo box, but only via a manual and fragile setup.
Can a QComboBox just automatically display the available pages?
Thank you
-
No, there is no standard way to do this. You will have to connect to a signal of the combobox and call some slot on the stacked widget. As an outline:
- "QStackedWidget::addWidget() ":http://doc.qt.nokia.com/4.7/qstackedwidget.html#addWidget returns an int - store that
- call "QComboBox::addItem() ":http://doc.qt.nokia.com/4.7/qcombobox.html#addItem and use the stored int for the user data of the new item
- connect a slot of your own to the signal "activated(int) ":http://doc.qt.nokia.com/4.7/qcombobox.html#activated of your combo box
- retrieve the user data (= index in stacked widget) with "QComboBox::itemData() ":http://doc.qt.nokia.com/4.7/qcombobox.html#itemData
- use the int to switch to the desired widget using "QStackedWidget::setCurrentIndex() ":http://doc.qt.nokia.com/4.7/qstackedwidget.html#currentIndex-prop