PDA

View Full Version : accessing subclass of QListBoxText



jcsneaks
5th February 2006, 15:09
Hello,

I have subclassed QLixtBoxText in order to add an integer value to the items for later processing.

When I want to access this value named "id" i.e. through AListBoxPointer->item(anumber)->id my compiler tells me this
"error: 'class QListBoxItem' has no member named 'id'"

when I override rtti to a different value it will give me this different value. But the compiler (maybe?) doesnt know my type?

Here's my code, i'm new (2 weeks) to qt, so don't be too hard on me ;)

programitem.h:
#include "qlistbox.h"

class ProgramItem: public QListBoxText {

public:
ProgramItem(const QString & text = QString::null);
~ProgramItem();
int rtti ();
int id;
};

programitem.cpp:
#include "programitem.h"
#include "qlistbox.h"

ProgramItem::ProgramItem( const QString & text) : QListBoxText(text)
{

}

int ProgramItem::rtti(){
return 1;
}


tried usage snippet with error:
ProgramItem *temp;
temp = new ProgramItem("teststring");
temp->id = 4 //just for test
available_programs->insertItem(temp); //available_programs is normal Listbox
int tester = available_programs->item(0)->id; // here comes the error

yop
5th February 2006, 15:42
int tester = available_programs->item(0)->id; // here comes the error
Why would you expect that available_programs->item(0) will return your subclassed QListBoxItem? It won't, see the documentation ;) You could cast the result to be of the subclassed type but it's not a clean solution and you might run into trouble but you can try it out

jcsneaks
5th February 2006, 19:09
I assumed it would return my class, since I inserted on abject of my class type beforehand. overriding rtti with a different value than the base type, also returns this different value. this is how I know, it actually is using my type. But I cant access my added integer called "id", because "item()" returns a normal QListboxitem.

Will try the typecast tomorrow, but what would be the/a clean way of doing it though then?

Would i also have to subclass qlistbox?
I basically just want to add an integer to the listitem.

yop
5th February 2006, 20:44
But I cant access my added integer called "id", because "item()" returns a normal QListboxitem.Exactly

Would i also have to subclass qlistbox?
Well that would be a clean solution ;)

jcsneaks
6th February 2006, 09:58
so i subclass qlistbox and override the item() function to return my class instead of qlistboxitem?

how would the code for that look like?

thanks for helping btw

wysota
6th February 2006, 10:09
I think you should stick with casting that listbox item to your class. Of course after checking the rtti(). If it matches, you can safely do the cast, if not, it means this item is not one of your custom ones.

jcsneaks
6th February 2006, 10:26
good idea with the rtti.
will try this later today, when I'm at home with the program.

why would you suggest this over subclassing qlistbox, just because it would be easier, and just as safe after checking rtti?
I would rather like to do it this way, because it also means I can keep using qtdesigner. WIth my own qlistbox subclass I would have to construct it on my own, set all the properties... etc.

will probably write this evening if everything worked out ok

wysota
6th February 2006, 10:29
Just because it is easier.

yop
6th February 2006, 18:20
Just because it is easier.
To tell you the truth I'd do the same thing ;)

jcsneaks
7th February 2006, 08:28
perfect.

works like a charm after fiddling just a bit with the typecast. (never done one before).

Thanks guys. Great help.
Will probably come by again with a new problem soon. Project isn't nearly finished yet ;)
(But which project ever is?)

-Jonathan