PDA

View Full Version : accessing my own class-instances



mikro
10th July 2006, 22:50
hi all,
i have filled a QListWidget with instances of my own class cMassnahme which inherits from QListWidgetItem.
Now i want to find all selected items and put their IDs (those are QStrings, i am not talking about their place within the QListWidget) into a QStringList called filterMassnahmen.
The function QListWidget::selectedItems() returns QListWidgetItems, so if i don't use it differently i won't be able to access my method getID() as this is not a method of QListWidgetItem. Therefore i tried

QList<cMassnahme *> items = listMassnahmen->selectedItems ();
for (int i = 0; i < items.size(); ++i) {
filterMassnahmen << items.at(i)->getID();
}
which gives me the message:

error: conversion from `QList<QListWidgetItem*>' to non-scalar type `QList<cMassnahme*>' requested
how else could i solve that? even if i tried to reimplement selectedItems() i would have to access QListWidgetItem::selectedItems() in there and still get to the same problem.

jacek
10th July 2006, 23:35
You can use a cast:
foreach( QListWidgetITem *item, listMassnahmen->selectedItems() ) {
cMassnahme * nameItem = dynamic_cast< cMassnahme * >( item );
if( nameItem != 0 ) {
filterMassnahmen << nameItem->getID();
}
else {
// error: not a cMassnahme?
}
}

mikro
10th July 2006, 23:46
Thank you, i have meanwhile found a similar solution (wondering that i was able to do something rather similar i went through the code there). Now i do:

filterMassnahmen.clear();
QList<QListWidgetItem *> items = listMassnahmen->selectedItems();
for (int i = 0; i < items.size(); ++i) {
cMassnahme* anl = (cMassnahme*) items.at(i);
filterMassnahmen << anl->getID();
}
so seems i can easily convert a single item to cMassnahme but not the complete QList. But i have to acknowledge i still don't understand this typecasting thingy... can you tell me where i can read about it?

jacek
11th July 2006, 00:10
can you tell me where i can read about it?
http://www.qtcentre.org/forum/f-c-programming-9/t-what-are-your-favourite-c-books-29.html

My personal favorite is "C++ Primer" by Stanley B. Lippman and Josee Lajoie.
There is also a book available on-line: http://mindview.net/Books/TICPP/ThinkingInCPP2e.html