Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Convert QString to string

    General and Desktop
    qstring qt5.6 convert string
    3
    5
    19677
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Punit
      Punit last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • Ni.Sumi
        Ni.Sumi last edited by Ni.Sumi

        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/

        ? 1 Reply Last reply Reply Quote 2
        • Punit
          Punit last edited by Punit

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

          1 Reply Last reply Reply Quote 0
          • ?
            A Former User @Ni.Sumi last edited by

            @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).

            1 Reply Last reply Reply Quote 0
            • Ni.Sumi
              Ni.Sumi last edited by

              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).

              1 Reply Last reply Reply Quote 1
              • First post
                Last post