Results 1 to 3 of 3

Thread: design help with QList in an interface

  1. #1
    Join Date
    Jan 2011
    Posts
    6
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default design help with QList in an interface

    Hi,

    I am new to using QT and am in need of some guidence.

    I want to create an interface that returns a list of pointers to objects, and then the classes that implement this interface should be able to both set and get the objects in the list.

    Where I am getting confused is that I want a general interface that allows me to be able to get any items from any of the derived classes.
    I don't understand how I should be handling converting the specialized datatypes like
    Qt Code:
    1. QList<newsChannelItem*> items;
    To copy to clipboard, switch view to plain text mode 
    back via the interface
    Qt Code:
    1. QList<QObject*> getItems();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class ChannelInterface
    2. {
    3. public:
    4. virtual QList<QObject*> getItems() = 0;
    5. }
    To copy to clipboard, switch view to plain text mode 
    classes that then implement this interface

    Qt Code:
    1. class storeChannel : public QObject, public ChannelInterface
    2. {
    3. QList<storeChannelItem*> items;
    4. public:
    5. QList<QObject*> getItems(); // I want to be able to return a list of storeChannelItem's
    6.  
    7.  
    8. class newsChannel : public QObject, public ChannelInterface
    9. {
    10. QList<newsChannelItem*> items;
    11. public:
    12. QList<QObject*> getItems(); // I want to be able to return a list of newsChannelItem's
    To copy to clipboard, switch view to plain text mode 

    I appreciate any help on this, as I am pretty stuck.

    thanks in advance

    Guus Davidson

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: design help with QList in an interface

    If both storeChannelItem and newsChannelItem are based on QObject, then just store a QObject in your storeChannel and newsChannel.
    Otherwise, why won't you return a list of storeChannelItems and newsChannelItems via getItems() ?

  3. #3
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: design help with QList in an interface

    You could use templates:

    Qt Code:
    1. template <typename T>
    2. class ChannelInterface
    3. {
    4. public:
    5. virtual QList<T*> getItems() = 0;
    6. };
    7.  
    8. class storeChannelItem
    9. {
    10. // ...
    11. };
    12.  
    13. class newsChannelItem
    14. {
    15. // ...
    16. };
    17.  
    18. class storeChannel : public QObject, public ChannelInterface<storeChannelItem>
    19. {
    20. QList<storeChannelItem*> items;
    21. public:
    22. QList<storeChannelItem*> getItems();
    23. };
    24.  
    25. class newsChannel : public QObject, public ChannelInterface<newsChannelItem>
    26. {
    27. QList<newsChannelItem*> items;
    28. public:
    29. QList<newsChannelItem*> getItems();
    30. };
    To copy to clipboard, switch view to plain text mode 

    EDIT: Don't forget to define a virtual destructor in your interface also:

    Qt Code:
    1. template <typename T>
    2. class ChannelInterface
    3. {
    4. public:
    5. virtual ~ChannelInterface() {}
    6. virtual QList<T*> getItems() = 0;
    7. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by helloworld; 7th February 2011 at 07:36.

  4. The following user says thank you to helloworld for this useful post:

    GuusDavidson (10th February 2011)

Similar Threads

  1. Cast QList<Foo*> to QList<const Foo*>
    By vfernandez in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2010, 16:04
  2. Replies: 4
    Last Post: 20th August 2010, 13:54
  3. Problem with design interface
    By tux-world in forum Newbie
    Replies: 5
    Last Post: 10th March 2010, 14:19
  4. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42
  5. Replies: 3
    Last Post: 5th October 2008, 23:41

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.