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 into std::string
Forum Updated to NodeBB v4.3 + New Features

Convert QString into std::string

Scheduled Pinned Locked Moved General and Desktop
7 Posts 6 Posters 12.6k Views 1 Watching
  • 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.
  • J Offline
    J Offline
    jk_mk
    wrote on 27 May 2011, 09:08 last edited by
    #1

    Hello,

    I am using a QString in a function named Filename. I want to use it us a global variable, but as a member of std::string library. Is it possible to make such converion? And if yes, then how?

    Thanks

    1 Reply Last reply
    0
    • E Offline
      E Offline
      evergreen
      wrote on 27 May 2011, 09:22 last edited by
      #2

      I looked once for it and didn't find.
      I changed all the String I used I my project into QString ; they are pretty handy.
      Can't you do the same?

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on 27 May 2011, 09:24 last edited by
        #3

        If you look at the "FAQs":http://developer.qt.nokia.com/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa you find something about string conversion

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on 27 May 2011, 09:38 last edited by
          #4

          You can use the conversion "toStdString":http://doc.qt.nokia.com/4.7/qstring.html#toStdString
          Typically I am using the std::string since I have other classes which are relying on stl. QString is in my software only with GUI used. The conversion works without problems.
          However, you have to make sure that Qt is configured with -stl in order to have the stl support.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jk_mk
            wrote on 27 May 2011, 10:02 last edited by
            #5

            Thanks the conversion toStdString solved my problem

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on 27 May 2011, 10:04 last edited by
              #6

              "This":http://developer.qt.nokia.com/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa is the FAQ you need to reference.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Djay96
                wrote on 22 Jul 2013, 14:23 last edited by
                #7

                I tried toStdString but its giving bad Pointer and program gets crashed.
                so i tried this

                QString fileName ;
                std::string stdFileName = fileName.toLocal8Bit().constData();

                and it work fine for me.

                1 Reply Last reply
                0

                • Login

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