Important: Please read the Qt Code of Conduct -

how to compare QStringList to std::string vector

  • how to compare a std::vector<string> to a QStringList

  • @Qt-Enthusiast

    Checkout the different conversion routines for QList and QVector There are a couple of options for conversions.

  • Though you can convert the QStringList to std::vector<QString> by using QStringList::toVector() and QVector::toStdVector.
    But there seems to be no single function to compare a std::string to a QString.
    So you need to write one such function.

  • Lifetime Qt Champion


    What exact comparison do you have in mind ? That they have the same content in the same order ? Or that they have all values in common ?

  • @SGaist

    alll values in common

  • Lifetime Qt Champion


    You cannot directly compare QString and std::string as the former is Unicode UTF-16 and the latter may have one of the 8 bit encodings.

    So you need to convert one of them anyway.

  • Moderators

    from what the others already said, you could do something like this:

    bool compareQStdString(const QString &str1, const std::string &str2){
        return (str1 == QString::fromStdString(str2));
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        QList<QString> qstrings({"a","b","c","d","e","f","g"});
        for(int i(0); i < qstrings.size() && i < strings.size(); i++){
            qDebug() << "Strings are at index"<< i << "are equal:" << compareQStdString(,;
        return a.exec();
    Strings are at index 0 are equal: true
    Strings are at index 1 are equal: true
    Strings are at index 2 are equal: false
    Strings are at index 3 are equal: true
    Strings are at index 4 are equal: false
    Strings are at index 5 are equal: true
    Strings are at index 6 are equal: false

    It's probably not the fastest or most elegant way to do it, but it works at least.

Log in to reply