[SOLVED]How to dynamic change the type of QWidget shown to the user
-
I am having a problem to make the QWidget change dynamic as the user change the index in combobox. I have a slot connected to the currentIndexChanged(int) from the combobox. This slot should change the widget shown to the user according to the index of combobox.
I tried the following code, but didn't work:
@void Form::setMetodoWidget(int index)
{
Form2* form2;// a different widget to show
if(index == 0 ){
ui->widget = new QWidget;} if(index == 1){ form2 = new Form2; ui->widget = form2; }
}@
I tried to add form2 in a vertical layout and then add to the widget, but then he gives this error message:
Attempting to set QLayout "" on QWidget "widget", which already has a layoutI would like to know how to do the "Promote to" function of QtDesigner,but in code.
Thanks. -
You must pass throught the layout:
http://qt-project.org/doc/qt-5/qlayout.html#removeWidget
and then add the new widget. Be careful to memory leak ;) -
Hop, even better way:
http://qt-project.org/doc/qt-5/qstackedlayout.html -
I would use a QStackedWidget. Then add all of the widgets you would like to display to it. Then you can like it to the combo box on change notification and change to the proper widget.