Results 1 to 6 of 6

Thread: std vs qt containers

  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

    Talking std vs qt containers

    I will have a function that places actions one by one into a "map" and then returns it.
    The receiving function will use different "lists" to loop over that map.

    Do you recommend QMap, QHash, std::map or std::unordered_map?

    For short lived lists, with no inserts or removals, that will be used once in a ranged for loop,
    do you recommend QList or std::vector?
    I considered a simple array, but I want to just keep reusing the same variable name.

    The lists will be used in combination with the map to created toolbars/menues in loops.

    example idea:

    Qt Code:
    1. vector<string> a_vector;
    2.  
    3. a_vector = {"a1", "a2", "a3"}
    4.  
    5. for (string str : a_vector)
    6. {
    7. a_map[str]->setCheckable(true);
    8. menu->addAction(a_map[str]);
    9. toolbar->addAction(a_map[str]);
    10. }
    11.  
    12. ... menu and toolbar change ...
    13.  
    14. a_vector = {"a4", "a5", "a6", "a7"}
    15.  
    16. for (string str : a_vector)
    17. {
    18. a_map[str]->setCheckable(true);
    19. menu->addAction(a_map[str]);
    20. toolbar->addAction(a_map[str]);
    21. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: std vs qt containers

    I personally choose to use Qt containers when writing stuff that uses Qt, but I think the std containers are slightly faster. For such a small, static list of items, I don't think it will matter.

    Edit: I would use QMap and QList for what you have stated you want to do.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: std vs qt containers

    (...) do you recommend QList or std::vector?
    If performance is not a concern, use the one you like better (API, personal taste etc.). If you are concerned about performance, then profile and choose the one better suited for the job.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: std vs qt containers

    I personally like my computational code to be even more portable than Qt, so I tend to use STL containers for most things. However, I have recently discovered that std::iterator is about 3 - 5 times slower in release mode and several orders of magnitude slower in debug mode than simple index access in Visual Studio 2013 because of extra bounds checking that goes on under the hood. I haven't done any comparable testing on Qt containers and iterators.

  5. #5
    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: std vs qt containers

    Don't use ranged for with a Qt container, at least not with non-const one.
    On a non-const container, ranged for will call non-const begin/end, which will cause a Qt container to deep copy if its reference counter is > 1.

    http://www.dvratil.cz/2015/06/qt-con...e-based-loops/

    In general, use vector over list, e.g. QVector or std::vector instead of QList, unless you need the list features.

    https://marcmutz.wordpress.com/effective-qt/containers/

    Cheers,
    _

  6. #6
    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: std vs qt containers

    Wow... there is a lot of information in those blogs.

    Thanks all.

Similar Threads

  1. Drawing rectangle containers ;
    By mut in forum Qt Programming
    Replies: 1
    Last Post: 25th June 2014, 07:09
  2. Qt containers
    By Ashish Bhasin in forum Newbie
    Replies: 5
    Last Post: 14th April 2014, 12:50
  3. Best way to represent struct using Qt containers
    By rajil.s in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2012, 18:30
  4. About STL containers and QT equivalent
    By tonnot in forum Qt Programming
    Replies: 7
    Last Post: 11th February 2011, 14:17
  5. Qt containers or STL containers?
    By sparticus_37 in forum Newbie
    Replies: 2
    Last Post: 11th May 2010, 15:12

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.