Feb 042016

I just read Jens Wellers article regarding raw loops vs. STL algorithms.

At the end he states “When I work with Qt, there are some cases, when you only get the count and a method to access item n, like in a QListWidget:” with the example:

And that is the point where I want bring in the possibility to use Qt widget container like objects in range based for loops or in STL algorithms. The only thing that one needs for this, is a small proxy that provides the begin(), end() interface:

So how to use it in the example from above?

For better readability and perhaps later reuse at other places let’s define this type:

And now the example becomes:

The template might be simplified in regard of the interface. But Visual Studio 2013, the compiler I have to use currently, lags certain C++11 features.

What do you think? So feedback is welcome.



  3 Responses to “How to bring Qt Widgets to STL algorithms”

  1. Pretty neat feature. Other then convenience maybe what would you say the application is for this?

  2. Hi,

    You do not really need a proxy, just non-member begin and end functions, and you will be able to do a range-for over that container without wrapping it inside something else. You can see an example of this at [1] which provides iterators and range-for for QSqlQuery.


    [1] https://quickgit.kde.org/?p=kactivities.git&a=blob&h=d43cce96ce148c0a3caed012cce3b4bcc975a055&hb=e030197846a6f781264f36f8499a191be8402bf6&f=src%2Futils%2Fqsqlquery_iterator.h

    • Felix Petriconi

      The QSQLQuery solutions is an other valid one. From my point of view, it is a matter of taste, which one favors.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">



This site uses Akismet to reduce spam. Learn how your comment data is processed.