Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Convert QString to string

Convert QString to string

Scheduled Pinned Locked Moved Solved General and Desktop
qstringqt5.6convertstring
5 Posts 3 Posters 22.8k Views
  • 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.
  • PunitP Offline
    PunitP Offline
    Punit
    wrote on last edited by
    #1

    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
    0
    • Ni.SumiN Offline
      Ni.SumiN Offline
      Ni.Sumi
      wrote on last edited by Ni.Sumi
      #2

      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
      2
      • PunitP Offline
        PunitP Offline
        Punit
        wrote on last edited by Punit
        #3

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

        1 Reply Last reply
        0
        • Ni.SumiN 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/

          ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          @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
          0
          • Ni.SumiN Offline
            Ni.SumiN Offline
            Ni.Sumi
            wrote on last edited by
            #5

            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
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved