Convert QString to std::string

Convert QString to std::string

When working with Qt, you often deal with QString, which is Qt's Unicode-aware string class. However, there might be situations where you need to interact with standard C++ libraries or codebases that utilize std::string. This tutorial explains how to convert QString to std::string.

The toStdString function provided by QString can be used to convert it to a std::string. This function internally uses the toUtf8 function for creating std::string, ensuring Unicode safety throughout the conversion process.

#include <QString>
#include <iostream>

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

    std::cout << str << std::endl;

    return 0;

Leave a Comment

Cancel reply

Your email address will not be published.