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. How to convert QString to const char *
Forum Updated to NodeBB v4.3 + New Features

How to convert QString to const char *

Scheduled Pinned Locked Moved General and Desktop
18 Posts 12 Posters 161.4k 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.
  • T Offline
    T Offline
    Thanatos.jsse
    wrote on last edited by
    #9

    [quote author="Gerolf" date="1301125342"]
    Take care of the lifetime of these variables![/quote]

    Ok Gerolf, I'll take care in the following contributions.

    Thanks.

    1 Reply Last reply
    1
    • S Offline
      S Offline
      shiroki
      wrote on last edited by
      #10

      check this http://developer.qt.nokia.com/faq/answer/how_can_i_convert_a_qstring_to_char_and_vice_versa

      blog: http://www.cuteqt.com/blog
      bbs: http://www.cuteqt.com/bbs

      1 Reply Last reply
      0
      • S Offline
        S Offline
        sumeetrt
        wrote on last edited by
        #11

        thank you sir it was helpful

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sumeetrt
          wrote on last edited by
          #12

          i want to type cast QByteArray to a QString
          @
          QByteArray myAppString[4096] ;
          QString myString ;
          @
          now i have some data in myAppstring now i want it to convert in QString
          so i have QString Constructor no 9 - which take const ByteArray
          but it is now working
          @
          QString *str1 = new QString(myAppString) ;
          @
          it give me error

          is there any way to do conversion of Qbytearray to QString

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #13

            A QByteArray is not much more than a nice wrapper around a char*, so the same rules apply.

            If you say that you get an error and you want help on that error, it is best to actually mention said error.

            1 Reply Last reply
            0
            • U Offline
              U Offline
              uranusjr
              wrote on last edited by
              #14

              QString Constructor No 9 (which takes a QByteArray) uses UTF-8, so you need to ensure that your byte array uses that encoding. It is better to use QString's static methods fromLatin1, fromLocal8Bit and fromUtf8 IMO because they are more explicit.

              You can check "the documentation":http://qt-project.org/doc/qt-5.0/qtcore/qstring.html for more detailed explanation.

              And I'd also like to recommend some "good reading on character encoding":http://www.joelonsoftware.com/articles/Unicode.html if you're not already familiar with its concept.

              1 Reply Last reply
              1
              • A Offline
                A Offline
                Alireza_13
                wrote on last edited by
                #15

                This can be used for eg. to send debugging information from your GUI to the console; since Debug requires a const char * :

                QString str = ui->lineEdit->text(); // lineEdit was already defined in a suitable context!
                Debug(&str.toStdString()[0]);

                Hope it worked!

                1 Reply Last reply
                0
                • Y Offline
                  Y Offline
                  yoavmil
                  wrote on last edited by
                  #16

                  qPrintable is the best way

                  1 Reply Last reply
                  0
                  • D dangelog

                    http://lmgtfy.com/?q=convert+qstring+to+const+char+*

                    M Offline
                    M Offline
                    MXXIV
                    wrote on last edited by
                    #17

                    @dangelog Actually, I was redirected here from google search, which makes your post look pretty retarded.

                    1 Reply Last reply
                    -3
                    • FranckynosF Offline
                      FranckynosF Offline
                      Franckynos
                      wrote on last edited by
                      #18

                      Just QString.c_str() it works 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