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.
  • P Offline
    P Offline
    Punit
    wrote on 27 May 2016, 09:19 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
    • N Offline
      N Offline
      Ni.Sumi
      wrote on 27 May 2016, 09:26 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 27 May 2016, 09:44
      2
      • P Offline
        P Offline
        Punit
        wrote on 27 May 2016, 09:37 last edited by Punit
        #3

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

        1 Reply Last reply
        0
        • N Ni.Sumi
          27 May 2016, 09:26

          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 27 May 2016, 09:44 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
          • N Offline
            N Offline
            Ni.Sumi
            wrote on 27 May 2016, 09:55 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

            2/5

            27 May 2016, 09:26

            • Login

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