Convert std::string to QString

Convert std::string to QString

In Qt development, QString serves as the primary string class due to its Unicode support and extensive functionality. However, when integrating Qt code with standard C++ libraries or systems relying on std::string, it becomes necessary to bridge the gap between these two string types. This tutorial explains how to convert std::string to QString.

The fromStdString function provided by QString can be used to convert std::string to QString. This function guarantees Unicode safety by internally using the fromUtf8 function.

#include <QString>
#include <QDebug>

int main()
    std::string str = "Hello world";
    QString qstr = QString::fromStdString(str);

    qDebug() << qstr;

    return 0;

Leave a Comment

Cancel reply

Your email address will not be published.