Results 1 to 3 of 3

Thread: QList class member and item operations

  1. #1
    Join Date
    Jan 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QList class member and item operations

    Hi all.
    i have a non-really-qt related newbie problem that you surely can solve easily.

    i have a class 'Model' which holds some members. one of thos is a QList.
    this is the code for that class:

    Header:
    Qt Code:
    1. #ifndef MODEL_H
    2. #define MODEL_H
    3.  
    4. #include <QList>
    5.  
    6. class Model
    7. {
    8. public:
    9. Model();
    10. QList<QString> list() const;
    11.  
    12. private:
    13.  
    14. QList<QString> m_list;
    15. };
    16.  
    17. #endif // MODEL_H
    To copy to clipboard, switch view to plain text mode 

    cpp
    Qt Code:
    1. #include "model.h"
    2. #include <QString>
    3.  
    4. Model::Model()
    5. {
    6. m_list.append(QString("item1"));
    7. m_list.append(QString("item2"));
    8. }
    9.  
    10. QList<QString> Model::list() const
    11. {
    12. return m_list;
    13. }
    To copy to clipboard, switch view to plain text mode 

    when i execute this snippet of code:

    Qt Code:
    1. Model m;
    2.  
    3. qDebug() << "Before clear()";
    4. for(int i=0;i<m.list().count();i++)
    5. {
    6. qDebug() << m.list().at(i);
    7. }
    8.  
    9. m.list().clear();
    10.  
    11. qDebug() << "After clear()";
    12. for(int i=0;i<m.list().count();i++)
    13. {
    14. qDebug() << m.list().at(i);
    15. }
    To copy to clipboard, switch view to plain text mode 

    i get this output:

    Qt Code:
    1. "Before clear()"
    2. "item1"
    3. "item2"
    4. "After clear()"
    5. "item1"
    6. "item2"
    To copy to clipboard, switch view to plain text mode 

    i think that the problem is the fact that calling m.list() returns me a copy of the QList instead of a reference to it.
    i think that one way to solve this is returning a pointer to the QList instead, but i'm wondering if there is another way to do this avoiding pointers.

    sorry for the long post and thanks if you read since here

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QList class member and item operations

    i think that the problem is the fact that calling m.list() returns me a copy of the QList instead of a reference to it.
    Yes it returns a copy as per the implementation you have done !
    What do you want reference for ? May be if you tell what you want to do more, we can suggest some better way.

  3. #3
    Join Date
    Jan 2010
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QList class member and item operations

    the needs i have on that member are:

    insert a new item in the list;
    get all items;
    delete all items;

    by now, i have those 3 functions exposed in 3 public methods in my model:

    insertItem(QString item); //note that the type is not QString normally
    items();
    clearItems();

    i think this is a fat interface, because the method items().clear() exists but "doesn't work" and clearItems() shouldn't be there.
    hope my needs are clear

Similar Threads

  1. Class member reference
    By waynew in forum Newbie
    Replies: 2
    Last Post: 20th December 2009, 01:38
  2. A class member has ran away?!
    By MIH1406 in forum Newbie
    Replies: 4
    Last Post: 24th August 2009, 23:21
  3. Using a QMutex as a static class member
    By emostar in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 13:48
  4. Replies: 2
    Last Post: 31st October 2007, 15:11
  5. Replies: 5
    Last Post: 14th July 2006, 22:42

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.