accessing my own class-instances
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
Code:
QList<cMassnahme *> items = listMassnahmen->selectedItems ();
for (int i = 0; i < items.size(); ++i) {
filterMassnahmen << items.at(i)->getID();
}
which gives me the message:
Code:
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.
Re: accessing my own class-instances
You can use a cast:
Code:
foreach( QListWidgetITem *item, listMassnahmen->selectedItems() ) {
cMassnahme * nameItem = dynamic_cast< cMassnahme * >( item );
if( nameItem != 0 ) {
filterMassnahmen << nameItem->getID();
}
else {
// error: not a cMassnahme?
}
}
Re: accessing my own class-instances
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:
Code:
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?
Re: accessing my own class-instances
Quote:
Originally Posted by mikro
can you tell me where i can read about it?
http://www.qtcentre.org/forum/f-c-pr...-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