Results 1 to 5 of 5

Thread: How get a reference to a QList

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default How get a reference to a QList

    Hi to all,
    in my program I need to use a QList<MyStructure *> list, so I declared one in the private section of my class. I also need a method to get it ( a reference to it ).
    This is some code:

    Qt Code:
    1. struct ScanRec;
    2.  
    3. class VistaIrisMatching : public QDialog
    4. {
    5. Q_OBJECT
    6.  
    7. public:
    8. VistaIrisMatching(QWidget *parent = 0, Qt::WFlags flags = 0);
    9. ~VistaIrisMatching();
    10. Ui::VistaIrisMatchingClass ui;
    11. m_scanRecList& getScanRecList(); // <------ get a reference to the list
    12.  
    13. public slots:
    14. void onUpdateDisplay( IplImage* );
    15. bool onLoadDBRecords();
    16.  
    17. private:
    18. CVistaCamera* m_vistaCamera;
    19. QSqlDatabase IrisDB;
    20. QTimer* m_timer;
    21. QList<ScanRec *> m_scanRecList; // <------- the list
    22. };
    To copy to clipboard, switch view to plain text mode 

    I don't understand why it doesn't compile and the compiler says that the error is in the
    Qt Code:
    1. m_scanRecList& getScanRecList();
    To copy to clipboard, switch view to plain text mode 

    Where am I wrong?
    Best Regards
    Franco Amato

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: How get a reference to a QList

    This:
    Qt Code:
    1. m_scanRecList& getScanRecList();
    To copy to clipboard, switch view to plain text mode 
    Makes no syntactical nor logical sense.
    When you declare a method, you declare a return TYPE to it, and you don't use a variable for that.
    Qt Code:
    1. QList<ScanRec *>& getScanRecList(); // <------ get a reference to the list
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

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

    franco.amato (20th January 2011)

  4. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    694
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    59
    Thanked 1 Time in 1 Post

    Default Re: How get a reference to a QList

    But then I would be sure that the returned reference it to my list


    Added after 5 minutes:


    Sorry I noticed now that I did a very big mistake mmmm my poor c++.
    Thanx again
    Last edited by franco.amato; 20th January 2011 at 17:00.
    Franco Amato

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: How get a reference to a QList

    If you return a non const reference, you could make the list public and skip the getter method.

  6. #5
    Join Date
    Apr 2010
    Posts
    769
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 94 Times in 86 Posts

    Default Re: How get a reference to a QList

    Quote Originally Posted by Lykurg View Post
    If you return a non const reference, you could make the list public and skip the getter method.
    Quite correct. It makes no sense to return a reference to an internal private object through an accessor function, since you're effectively granting public access to the object by doing so.

Similar Threads

  1. Cast QList<Foo*> to QList<const Foo*>
    By vfernandez in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2010, 17:04
  2. Replies: 4
    Last Post: 20th August 2010, 14:54
  3. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 19:27
  4. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 09:42
  5. QList<QList > question
    By SubV in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 16:14

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
  •  
Qt is a trademark of The Qt Company.