Create arrays of widgets dynamically at runtime with the purpose of adding them to the window
-
Hello, I am just starting with Qt for a simple application. Although I got to know the very basics I run into a problem. At runtime the user will be presented with a spinbox and a button, depending on the number that the user has selected in the spinbox, when they click on the putton there are going to be spinboxes with labels on them. For example, the user sets teh spinbox to be equal to 10, then by clicking on the button they will get 10 spinboxes with each one's number on top of them.
Here is something I tried but had messed up layout:int hiddenLayers = ui->layersNum->value()-2; QSpinBox* hiddenBoxes[hiddenLayers]; QLabel* hiddenLabels[hiddenLayers]; QVBoxLayout* hiddenLayouts[hiddenLayers]; for(int i = 0; i< hiddenLayers; i++){ hiddenLayouts[i] = new QVBoxLayout; hiddenLabels[i] = new QLabel; hiddenLayouts[i]->addWidget(hiddenLabels[i],1); hiddenLabels[i]->setText("layerNum"); hiddenLabels[i]->show(); hiddenBoxes[i] = new QSpinBox; hiddenLayouts[i]->addWidget(hiddenBoxes[i],0); hiddenBoxes[i]->show(); ui->gridLayoutHidden->addLayout(hiddenLayouts[i],i%2,i,10,10,0); }
Here is the result: Program Screenshot
-
@AthanD said:
At runtime the user will be presented with a spinbox and a button, depending on the number that the user has selected in the spinbox, when they click on the putton there are going to be spinboxes with labels on them.
But why do you subtract 2 from the entered value?
The issue is pretty much caused by the line:
ui->gridLayoutHidden->addLayout(hiddenLayouts[i],i%2,i,10,10,0);
I think you mixed up some parameters.
Why do you modulo the row value by 2?
Why a row- and col-span value of 10? -
About the first question this is just how it works in the program, it doesn't have to do with the layout. I just need the number the user gives minus 2.
About the second question, if I would leave it like this : addLayout(hiddenLayouts[i]); , then it wouldn't work. I tried other values and I couldn't figure it out.
-
@AthanD said:
About the first question this is just how it works in the program, it doesn't have to do with the layout. I just need the number the user gives minus 2.
then you should make sure the user can't enter a value less than 3.
Why do you use a gridlayout?
Does the following do what you want?
ui->gridLayoutHidden->addLayout(hiddenLayouts[i],0,i);
-
@AthanD
actually IMO i believe the grid layout is the most complicated one offered by Qt :)
All the others are pretty much straight forward.But since you anyway want to display them in a grid it's your way to go ;)