Results 1 to 3 of 3

Thread: how to pass a QList pointer

  1. #1
    Join Date
    Sep 2012
    Posts
    31
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default how to pass a QList pointer

    Hi, I would like to ask how to properly pass a QList to another function? I have researched and discovered that QLists must be passed via pointer.

    For example:
    I have a qlist of plugins

    QList<PluginInterface *> *PluginList;

    and declared and instantiated it in this manner PluginList = new QList<PluginInterface *>();



    ..the function that will receive this list has a function prototype of

    void receive_qlist(QList<PluginInterface> *);


    ..is this method correct and efficient?

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: how to pass a QList pointer

    Just pass PluginList to the function is not working? If so tell us the error. One error is the function prototype that receives QList of PluginIntergface objects not pointers, so either use void receive_qlist(QList<PluginInterface*> *); //notice the extra * or declare your QList to hold objects: QList<PluginInterface> *PluginList; - depending on what you really want.

    But QList doesn't really need to be allocated on heap, so allocate on heap only if you really need that (and if you go with QList of pointers don't forget to delete all the pointers from QList when before you delete the QList pointer), so you can do just QList<PluginInterface*> pluginList; (now the QList is on the stack - maybe a member into an object that lives as long as that QList is needed)

    As for passing the QList as parameter read about implicit sharing of some Qt classes here and decide what you really need to do to the QList in the function so depending on that you can choose to pass a QList& or a const QList& or if you need QList* or just a QList and let the implicit sharing work (read the documentation linked above because there are cases when a deep copy will be made - this might affect performance or the logic of your application - depending on data type that you use in the list)
    Last edited by Zlatomir; 4th November 2012 at 08:34.

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: how to pass a QList pointer

    Quote Originally Posted by ehnuh View Post
    Hi, I would like to ask how to properly pass a QList to another function? I have researched and discovered that QLists must be passed via pointer.
    wrong. QLists can be passed by value or by reference or via indirection (pointer).

    pick your poison:

    void func(QList<int> list) // func will use a copy of list
    void func(QList<int>& list) // func will use the same instance of list
    void func(QList<int> const & list) // func will use the same instance of list and will not* modify it
    void func(QList<int>* list) // func will take a pointer to a QList<int> (that may be valid or not...)

    PS
    This is a general programming question, not a Qt programming question as you are merely asking about c++ syntax
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. The following 2 users say thank you to amleto for this useful post:

    Seishin (18th April 2018), TheIndependentAquarius (23rd December 2015)

Similar Threads

  1. Replies: 1
    Last Post: 4th December 2010, 17:20
  2. Replies: 4
    Last Post: 20th August 2010, 13:54
  3. Replies: 7
    Last Post: 8th August 2010, 09:05
  4. Replies: 14
    Last Post: 1st December 2009, 20:45
  5. QList<pointer>::takeFirst() and memory leak question
    By AcerExtensa in forum Qt Programming
    Replies: 3
    Last Post: 9th July 2009, 13:20

Tags for this Thread

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.