PDA

View Full Version : Add item to diferent listbox on loop



ShapeShiftme
17th May 2012, 03:48
Good Day All

I have 20 lis boxes in my ui.
named:

in_1
in_2
in_3
....
in_20

now im trying to add items dynamically this is what i would like to do but its not working



int i;
for(i=1;i<=20;i++)
{
QString boxtoinput = "in_" + QString::number(i);
ui->boxtoinput->addItem(QString::number(i));
}



The hope would be the above code will write the number of the box into each box.

Any ideas of how i would do this
i know i could write an if statement for each box but then i can genrate the forms dynamically.

Regards

Added after 12 minutes:

Another idea i had would be to loop through using this


QList<QListWidget *> allPButtons = this->findChildren<QListWidget *>(boxtoinput);

that returns

QListWidget(0x287daf0, name = "in_5")

Now with that how does one add add an item to it

Regards

ChrisW67
17th May 2012, 08:40
Now with that how does one add add an item to it
By dereferencing the pointer to your QListWidget to access its member functions.

Finding the widgets this way is OK if you are do not particularly care about order. You could programmatically create the QListWidgets and insert them into your layout in order to be sure of order.

Then again 20 lists in the one layout sounds horrible.

ShapeShiftme
19th May 2012, 08:09
This is how i ended getting it working

I hope this helps someone



int i;
for(i=1;i<=20;i++)
{
QString boxtoinput = "in_" + QString::number(i);
QListWidget* mylst = findChild<QListWidget*>(boxtoinput);
mylst->addItem(QString::number(daycount));
}

Spitfire
21st May 2012, 17:17
It would be much easier and faster for you to use simple QMap< int, QListWidget*> to store the lists and get them from there.