Convert QString to string



  • Hello,
    I have a Qstring which contains the path of a folder. I want to convert it into string.

    QString name = "/Users/macwaves/Desktop/copy to device2"
    string str= name. toStdString();
    std::cout<<str;
    

    OUTPUT: "copy to device2"

    I tried everything toUtf8().constData(), toLatin1().data(), toStdString but it produces the same output (Only show "copy to device2" instead of "/Users/macwaves/Desktop/copy to device2").

    Any Suggestions?



  • Hi @Punit ,

    Try this,

    QString name = "/Users/macwaves/Desktop/copy to device2";
    std::string text = name.toUtf8().constData();
    std::cout<< text;

    or

    QString name = "/Users/macwaves/Desktop/copy to device2";
    std::string text = name.toLocal8Bit().constData();
    std::cout<< text;

    In my case,both worked well. I have tested. http://postimg.org/image/rn4sm30sb/



  • Yes, It works. Thanks a lot buddy. @Ni-Sumi:)



  • @Ni.Sumi Do you know why the OP's code doesn't work for him? It works for me and I don't see anything wrong with his code (besides typos in the post).



  • Hi @Wieland ,

    I did not test the code. But Yes, you are right. I tested @Punit 's code after my comment. It worked for me too(missed only that semicolon).


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.