Displaying data from object selected od QListWidget
-
I have a class T with some data and QString NAME. I keep T class objects in a QList<T>.
First thing I want to do is to print all of the list items by its NAME using QListWidget and its function addItem (using foreach). I believe this solution is not that bad but problem is for my next goal:* I want to print (in other widget) some stored data for T object cliked on that QListWidget*. My first idea is to go through that QList<T> again (using foreach) and compare selected string (from QListWidget) with NAME string fetched one by one from QList<T>. I believe there's the right way because my sounds very bad!I have Qt version 4.8.5 and I'm not familiar with QListView etc.
-
Hi,
I would use std::map instead of QList and use the find function
@
map<NAME, T> mList;
T t1, t2, t3;
t1.setName("T1");
t2.setName("T2");
t3.setName("T3");mList[t1.name()] = t1;
mList[t2.name()] = t2;
mList[t3.name()] = t3;// instead of t2.name() use the Name of the selecteditem.
T listItem = mList.find(t2.name())->second;
@Hope that helps.
-
Ok thanks but why there is no pure Qt solution? I mean there's std::map used instead of QMap (I see there is no setName() in Qt)
-
Hi and welcome to devnet,
Since you didn't share any code, sw0ce used setName as a mean to update the name variable from your class T. It's nothing Qt specific. However you are right about QMap, it would make more sense.