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.



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

  1. 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.

Sorry, the comment form is closed at this time.