How to convert QString to std::string ?

How to convert QString to std::string ?

Asked on December 14, 2018 in Qt.
Add Comment


  • 3 Answer(s)

    convert the QString to std::string:

    QString qs;  
     
    // Either this if you use UTF-8 anywhere
    std::string utf8_text = qs.toUtf8().constData();
     
    // or this if you're on Windows 
    std::string current_locale_text = qs.toLocal8Bit().constData();
    

    Try this link:

    http://doc.qt.io/qt-5/qstring.html#toLatin1

    Answered on December 14, 2018.
    Add Comment

    Just convert QString to std::string:

    QString qs;
    // do things
     
    std::cout << qs.toStdString() << std::endl;
    
    Answered on December 14, 2018.
    Add Comment

    Debugging messages to the console, you can use qDebug().

     qDebug()<<string; which will print the contents to the console.

    This is the easy way  to convert std::string for debugging messages

    Answered on December 14, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.