PDA

View Full Version : add multiple QCoombox using loop



uz_qt
26th September 2013, 11:47
Hello,

How can one add multiple QComboBoxes using a for-loop?

Like we do in qcheckbox as:

for (int k=0; k<3 ; k++)
{
mCheckBox = new QCheckBox(tr("input %2:").arg(k));
}

Is there a similiar way to add multiple QComboBoxes aswell?

PS: i could not find the .arg() object in QComboBox :-(

Thanks!

wysota
26th September 2013, 12:03
arg() is a method of QString.

uz_qt
26th September 2013, 12:39
Okay, then what is the way of doing it for QComboBox?

I have a combobox next to checkbox. For checkbox I used the code as mentioned above.
Now I want to repeat this combination using a for-loop.

How can this be achieved?

wysota
26th September 2013, 13:18
for(int i=0;i<n;++i) {
QComboBox *cb = new QComboBox;
// ...
}

uz_qt
26th September 2013, 13:45
@wysota
I have already tried this. But the comboBoxes are not repeated. Only the checkboxes are repeated. Following is my code:


QGridLayout* ChannelLayout1 = new QGridLayout(this);
QGroupBox *GroupBox1 = new QGroupBox(tr("Box"));

for(k=0; k < 5 ; k++)
{
QCheckBox *mCheckBox1 = new QCheckBox(tr("input %2:").arg(k));
QComboBox *mComboBox1 = new QComboBox;

for(int i=0; i < 3; i++)
{
mComboBox1->addItem(tr("Item %2").arg(i));
}

mComboBox1->setFixedWidth(200);
ChannelLayout1->addWidget(mCheckBox1);
ChannelLayout1->addWidget(mComboBox1,0,1,0,20);
}
GroupBox1->setLayout(ChannelLayout1);

Please help.

wysota
26th September 2013, 14:56
You are putting all comboboxes in one place in the layout.

uz_qt
26th September 2013, 17:24
@wysota

Ya, i figured that out. There was one more problem besides that in lines 15 and 16. The corrected lines are as follows:


15. ChannelLayout1->addWidget(mCheckBox1,k,0);
16. ChannelLayout1->addWidget(mComboBox1,k,1);

Thanks!

uz_qt
27th September 2013, 12:17
@wysota:

Thanks for ur advice. I have sorted it out! :-)

wysota
27th September 2013, 12:24
You are trying to set the same layout to two widgets.