Convert std::vector to QList

Convert std::vector to QList

When working with C++ Qt applications, you may encounter situations where you need to convert data between different container types. One common scenario is converting a std::vector to a QList. This conversion can be necessary when interfacing with Qt's APIs or when transitioning between standard C++ and Qt-based code. This tutorial shows how to convert std::vector to QList.

The following code snippet initializes a std::vector with three elements. It then constructs a QList using the range constructor, which takes iterators pointing to the beginning and end of the std::vector. Finally, the contents of the QList are printed to the terminal.

#include <QList>
#include <QDebug>

int main()
{
    std::vector list{"First", "Second", "Third"};
    QList qlist(list.begin(), list.end());

    qDebug() << qlist;

    return 0;
}

Leave a Comment

Cancel reply

Your email address will not be published.