Results 1 to 4 of 4

Thread: for loop and QList<QAction*>

  1. #1
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default for loop and QList<QAction*>

    Given a QList<QAction*>
    do you recommend writing a for loop as:
    for(auto const& item: list)

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: for loop and QList<QAction*>

    You can do that.

    If "list" is not const this will call non const versions of begin() and end(), causing the QList to detach if the reference count is greater than 1, i.e. create a "deep copy".

    Cheers,
    _

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

    ravas (12th October 2015)

  4. #3
    Join Date
    Jun 2015
    Location
    California, USA
    Posts
    61
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: for loop and QList<QAction*>

    Thanks. How would you write it if the list is not const?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: for loop and QList<QAction*>

    You either make it const
    Qt Code:
    1. const QList<QAction*> constList = list;
    2. for (.... : constList)
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. for (.... : const_cast<const QList<QAction*>& >(list))
    To copy to clipboard, switch view to plain text mode 
    or you use Qt's foreach
    Qt Code:
    1. foreach (const QAction* action, list)
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    ravas (12th October 2015)

Similar Threads

  1. For loop and While loop handling.
    By kiboi in forum Newbie
    Replies: 7
    Last Post: 2nd January 2013, 13:43
  2. Qlist<QLabel *> in Qlist<QAction*>
    By Naahmi in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2011, 08:53
  3. Replies: 4
    Last Post: 6th August 2011, 01:40
  4. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  5. Replies: 4
    Last Post: 20th August 2010, 13:54

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.