PDA

View Full Version : QListWidget selected item Row id.



nagabathula
20th December 2010, 11:04
Hello i have a small problem... I am populating a QListWidget with some names like name1 to name300, now when i select a few names from this list say from name1 to name5 and name45 to name80 , i need to create a relavent TagId some thing like TAGID_1 for the selected items in ListWidget.. I was thinking if i know the row id's of the selected names in the ListWidget i can append that row id number to the TagId and i could get the right sequence in how i have selected the items in the ListWidget.


QList<QListWidgetItem *>item = NewLISTLW->selectedItems(); // NewLISTLW is Obj name for ListWidget 1
for(kfast = 0;kfast<item.count();kfast++)//insert selected items from list to selected another list.
{
QListWidgetItem * item1 = new QListWidgetItem(item[kfast]->text());
SelectedLW->insertItem(kfast,item1); // SelectedLWis Obj name for ListWidget 2
selectedlist.append(SelctdPresParamsLW->item(kfast)->text());
}
for(int k=0; k< selectedlist.count(); k++)
{
tagidnames.append(QString("TAGID_%1").arg(k)); // QVector<QString> selectedlist,tagidnames;
}
qDebug() << tagidnames;
but here the TAGID is created for how many items are selected not in relation with rowid.
if i select name1 to name5 and name45 to name50 instead of creating TAGID1 to TAGID5 and TAGID45 to TAGID50 .. i can see from TAGID1 to TAGID10.. how can i use the selected row id to get what i want here.. ?

thank you

high_flyer
20th December 2010, 11:41
but here the TAGID is created for how many items are selected not in relation with rowid.
if i select name1 to name5 and name45 to name50 instead of creating TAGID1 to TAGID5 and TAGID45 to TAGID50 .. i can see from TAGID1 to TAGID10..
This is also what I would expect from your code to do.
Your 'item' list as as long as the number of selected items, and the index of thelist has nothing to do with the index of your items in the original list from you you have selected the items from.
You need to retrieve the index of the items from the original list.



for(kfast = 0;kfast<item.count();kfast++)//insert selected items from list to selected another list.
{
QListWidgetItem * item1 = new QListWidgetItem(item[kfast]->text());
SelectedLW->insertItem(kfast,item1); // NewLISTLW is Obj name for ListWidget 2
selectedlist.append(SelctdPresParamsLW->item(kfast)->text());

tagidnames.append(QString("TAGID_%1").arg(NewLISTLW->row(item1)));
}
//for(int k=0; k< selectedlist.count(); k++)
//{
// tagidnames.append(QString("TAGID_%1").arg(k)); // QVector<QString> //selectedlist,tagidnames;
//}



Please note I didn't test compile it.

nagabathula
20th December 2010, 15:15
Hello sir thank you so much for the reply.. i did compile the code you gave i have one error..
for the below code

tagidnames.append(QString("TAGID_%1").arg(NewLISTLW->row(item1)));


QListWidget::row' : cannot convert parameter 1 from 'QList<T>' to 'const QListWidgetItem *'
with
[
T=QListWidgetItem *
]


thank you

high_flyer
20th December 2010, 15:22
Then cast the item!

nagabathula
20th December 2010, 15:34
hello sir can you please show me a small example of how to do it exactly.. i am not very sure with how to cast this is the first time i am doing something like this..

Thank you

high_flyer
20th December 2010, 15:40
tagidnames.append(QString("TAGID_%1").arg(NewLISTLW->row((const QListWidgetItem *)item1)));

nagabathula
20th December 2010, 16:43
hello sir i compiled the code but i don't get the desired out put in QDebug..
this is what i am getting in case i selected name1 to name3 and name5 to name7 ..

QVector("TAGID_-1", "TAGID_-1", "TAGID_-1", "TAGID_-1", "TAGID_-1", "TAGID_-1", "TAGID_-1")

sir i read in qt assistant , i think we won't get the actual row value from this


QList<QListWidgetItem *> QListWidget::selectedItems () const
Returns a list of all selected items in the list widget

since i am coping all selected list from the NewLISTLW, can i get the actual index pointer so that i can use that to print the TAGID_ for the relevant selected sequence in the List.

QList<QListWidgetItem *>item = NewLISTLW->selectedItems();
thank you

high_flyer
20th December 2010, 16:58
Try this:


tagidnames.append(QString("TAGID_%1").arg(NewLISTLW->row(item.at(kfast))));

nagabathula
20th December 2010, 17:08
thank you so much sir.. :) problem is solved this worked.. i think i understand what we are doing here.. but i din't understand the previous suggestion.
thanks for your time you helped me solve my problem..

Regards