How to dynamically create varying number of widgets based on user input
-
Hello. In my program, I would like to create different number of radiobuttons.
At the moment, I manually create 3 radio buttons using:
in my header I declare the widgets:
QScrollArea *scrollArea; QWidget *scrollAreaWidgetContents; QPushButton *start_test_button; QRadioButton *radioButton; QRadioButton *radioButton_2; QRadioButton *radioButton_3; QRadioButton *radioButton_4;
in my cpp:
void TestTool::Create_widgets(QMainWindow *parent){ scrollArea = new QScrollArea(parent); scrollArea->setObjectName(QString::fromUtf8("scrollArea")); scrollArea->setGeometry(QRect(30, 110, 481, 371)); scrollArea->setWidgetResizable(true); scrollAreaWidgetContents = new QWidget(); scrollAreaWidgetContents->setObjectName(QString::fromUtf8("scrollAreaWidgetContents")); scrollAreaWidgetContents->setGeometry(QRect(0, 0, 479, 369)); radioButton = new QRadioButton(scrollAreaWidgetContents); radioButton->setObjectName(QString::fromUtf8("radioButton")); radioButton->setGeometry(QRect(50, 50, 150, 20)); radioButton_2 = new QRadioButton(scrollAreaWidgetContents); radioButton_2->setObjectName(QString::fromUtf8("radioButton_2")); radioButton_2->setGeometry(QRect(50, 100, 150, 20)); radioButton_3 = new QRadioButton(scrollAreaWidgetContents); radioButton_3->setObjectName(QString::fromUtf8("radioButton_3")); radioButton_3->setGeometry(QRect(50, 150, 150, 20)); scrollArea->setWidget(scrollAreaWidgetContents); start_test_button = new QPushButton(scrollAreaWidgetContents); start_test_button->setObjectName(QString::fromUtf8("start_test_button")); start_test_button->setGeometry(QRect(50, 20, 150, 24)); start_test_button->setText(QCoreApplication::translate("Widget", "Start test", nullptr)); radioButton->setText(QCoreApplication::translate("Widget", "test1", nullptr)); radioButton_2->setText(QCoreApplication::translate("Widget", "test2", nullptr)); radioButton_3->setText(QCoreApplication::translate("Widget", "test3", nullptr)); scrollArea->show(); }
and it works fine:
However, instead of manually creating specific amount of radiobutton widgets, I need to be able to create large amount of radiobuttons (up to 100) based on user choice. In my program, user can select a specific testing scenario that he wants to perform and based on this selection I will determine how many unique tests will need to be performed and for each unique test I need to create radiobutton.
So lets say I have a list of structure:
struct dynamic_widget_s{ Qstring widget_button_name; uint8_t widget_number; }; QList<dynamic_widget_s> widget_struct;
Imagine I have a list of 10 structures:
widget_struct[0].button_name = "button1"; widget_struct[0].widget_number = 1; widget_struct[1].button_name = "button2"; widget_struct[1].widget_number = 2; widget_struct[2].button_name = "button3"; widget_struct[2].widget_number = 3; widget_struct[3].button_name = "button4"; widget_struct[3].widget_number = 4; widget_struct[4].button_name = "button5"; widget_struct[4].widget_number = 5; widget_struct[5].button_name = "button6"; widget_struct[5].widget_number = 6; widget_struct[6].button_name = "button7"; widget_struct[6].widget_number = 7; widget_struct[7].button_name = "button8"; widget_struct[7].widget_number = 8; widget_struct[8].button_name = "button9"; widget_struct[8].widget_number = 9; widget_struct[9].button_name = "button10"; widget_struct[9].widget_number = 10;
and I want to create 10 unique radiobuttons and display them on my application. Could someone point me in the right direction and suggest me a best way to do this?
Keep in mind that the structure is assigned only during runtime of the application so the number of widgets that I will need to create may vary.
-
@lukutis222 said in How to dynamically create varying number of widgets based on user input:
Could someone point me in the right direction and suggest me a best way to do this?
What exactly is the problem?
You create a new button like this:QPushButton *newButton = new QPushButton(QIcon(), widget_struct[i].button_name, this); // Now add it to a layout where you want to have it
-
@jsulm
Thanks for the response.
I still do not understand how can I dynamically create this.
Lets say I want to create 10 radiobuttons:for(int i=0; i < 10; i ++){ QRadioButton *radiobutton = new QRadioButton(widget_struct[i].button_name, this); }
The code above will not work becuase the QRadioButton object (radiobutton) will be overwritten 10 times.
How can I ensure that I have unique object for each button?I cant simply do:
QRadioButton *radiobutton1 = new QRadioButton(widget_struct[0].button_name, this); QRadioButton *radiobutton2 = new QRadioButton(widget_struct[1].button_name, this); QRadioButton *radiobutton3 = new QRadioButton(widget_struct[2].button_name, this); QRadioButton *radiobutton4 = new QRadioButton(widget_struct[3].button_name, this); QRadioButton *radiobutton5 = new QRadioButton(widget_struct[4].button_name, this); QRadioButton *radiobutton6 = new QRadioButton(widget_struct[5].button_name, this); QRadioButton *radiobutton7 = new QRadioButton(widget_struct[6].button_name, this); QRadioButton *radiobutton8 = new QRadioButton(widget_struct[7].button_name, this); QRadioButton *radiobutton9 = new QRadioButton(widget_struct[8].button_name, this); QRadioButton *radiobutton10 = new QRadioButton(widget_struct[9].button_name, this);
Unless I know how many buttons I am going to have before runtime. Unfortunately I do not because that will depend on user choise.
What you suggested is simply manually creating button. What if I need to create 100 of them?
-
@lukutis222 If you don't need to hold on to them then just create the button in a loop and use it immediately e.g.
for(int i=0; i < 10; i ++){ QRadioButton *radiobutton = new QRadioButton(widget_struct[i].button_name, this); // for example: someWidget->layout()->addWidget(radiobutton); connect(radiobutton, &QRadioButton::toggled, something, &SomeClass::someSlot); }
If you need to hold on to the pointer for something just put them in a container:
QVector<QRadioButton*> buttons; for(int i=0; i < 10; i ++){ QRadioButton *radiobutton = new QRadioButton(widget_struct[i].button_name, this); buttons << radiobutton. }
and you can then use them as
buttons[42]
or iterate over the vector or do whatever you want with them. -
I suggest to add a variable to the struct:
struct dynamic_widget_s{ Qstring widget_button_name; uint8_t widget_number; QRadioButton* button; }; for(int i=0; i < 10; i ++){ struct[i].button = new QRadioButton(widget_struct[i].button_name, this); }
-
@Chris-Kawa @mpergand
Both great solutions. I do not know why I didint think about that.. Thank you very much!