Results 1 to 10 of 10

Thread: accessing subclass of QListBoxText

  1. #1
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Unhappy accessing subclass of QListBoxText

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing subclass of QListBoxText

    Quote Originally Posted by jcsneaks
    Qt Code:
    1. int tester = available_programs->item(0)->id; // here comes the error
    To copy to clipboard, switch view to plain text mode 
    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

  3. #3
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: accessing subclass of QListBoxText

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing subclass of QListBoxText

    Quote Originally Posted by jcsneaks
    But I cant access my added integer called "id", because "item()" returns a normal QListboxitem.
    Exactly
    Quote Originally Posted by jcsneaks
    Would i also have to subclass qlistbox?
    Well that would be a clean solution
    Last edited by yop; 5th February 2006 at 19:46.

  5. #5
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: accessing subclass of QListBoxText

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: accessing subclass of QListBoxText

    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.

  7. #7
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: accessing subclass of QListBoxText

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: accessing subclass of QListBoxText

    Just because it is easier.

  9. #9
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing subclass of QListBoxText

    Quote Originally Posted by wysota
    Just because it is easier.
    To tell you the truth I'd do the same thing

  10. #10
    Join Date
    Feb 2006
    Posts
    5
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: accessing subclass of QListBoxText

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.