Results 1 to 4 of 4

Thread: accessing my own class-instances

  1. #1
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. QList<cMassnahme *> items = listMassnahmen->selectedItems ();
    2. for (int i = 0; i < items.size(); ++i) {
    3. filterMassnahmen << items.at(i)->getID();
    4. }
    To copy to clipboard, switch view to plain text mode 
    which gives me the message:
    Qt Code:
    1. error: conversion from `QList<QListWidgetItem*>' to non-scalar type `QList<cMassnahme*>' requested
    To copy to clipboard, switch view to plain text mode 
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: accessing my own class-instances

    You can use a cast:
    Qt Code:
    1. foreach( QListWidgetITem *item, listMassnahmen->selectedItems() ) {
    2. cMassnahme * nameItem = dynamic_cast< cMassnahme * >( item );
    3. if( nameItem != 0 ) {
    4. filterMassnahmen << nameItem->getID();
    5. }
    6. else {
    7. // error: not a cMassnahme?
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to jacek for this useful post:

    mikro (11th July 2006)

  4. #3
    Join Date
    Apr 2006
    Location
    Erlangen, Germany
    Posts
    58
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. filterMassnahmen.clear();
    2. QList<QListWidgetItem *> items = listMassnahmen->selectedItems();
    3. for (int i = 0; i < items.size(); ++i) {
    4. cMassnahme* anl = (cMassnahme*) items.at(i);
    5. filterMassnahmen << anl->getID();
    6. }
    To copy to clipboard, switch view to plain text mode 
    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?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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

Similar Threads

  1. Replies: 2
    Last Post: 4th May 2006, 20:17
  2. Qt in other class
    By Morea in forum Qt Programming
    Replies: 4
    Last Post: 9th April 2006, 18:08
  3. How to propagate from one class to another
    By mahe2310 in forum Qt Programming
    Replies: 15
    Last Post: 20th March 2006, 02:27
  4. Replies: 5
    Last Post: 15th March 2006, 08:33
  5. virtual overloaded functions and base class function call...
    By nouknouk in forum General Programming
    Replies: 7
    Last Post: 11th March 2006, 22:26

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.