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. Qt5 convert QString into char [] or char *

Qt5 convert QString into char [] or char *

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 6 Posters 25.5k 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.
  • Y Offline
    Y Offline
    yczo
    wrote on 10 May 2016, 13:09 last edited by A Former User 5 Dec 2016, 07:49
    #1

    Hello, I googled a lot over that theme, all deprecated or do not work

    QString hello = "Hello World"; ---> to char *cHello or to char cHello[30]

    How to convert in Qt5 to a standard c++ char array?

    i tried for(int i = 0; ;i++) cHello[i] = hello.at(i); .... and much more, but without sucess

    Someone kind enough to help? please help :-)

    thanks in advance. greetings

    A 1 Reply Last reply 10 May 2016, 13:13
    0
    • Y yczo
      10 May 2016, 13:09

      Hello, I googled a lot over that theme, all deprecated or do not work

      QString hello = "Hello World"; ---> to char *cHello or to char cHello[30]

      How to convert in Qt5 to a standard c++ char array?

      i tried for(int i = 0; ;i++) cHello[i] = hello.at(i); .... and much more, but without sucess

      Someone kind enough to help? please help :-)

      thanks in advance. greetings

      A Offline
      A Offline
      Aggs
      wrote on 10 May 2016, 13:13 last edited by
      #2

      @yczo hello.ToStdString().c_str()

      1 Reply Last reply
      1
      • Y Offline
        Y Offline
        yczo
        wrote on 10 May 2016, 13:21 last edited by yczo 5 Oct 2016, 13:22
        #3

        Thank you very much for answer, but i think that this is deprecated method. :-(
        When I run
        char *cHello = hello.ToStdString().c_str();

        get the next error:

        error: C2039: 'ToStdString' : is not a member of 'QString'

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on 10 May 2016, 13:24 last edited by
          #4

          Just a typo. It's std::string QString::toStdString() const.

          1 Reply Last reply
          2
          • S Offline
            S Offline
            sneubert
            wrote on 10 May 2016, 13:25 last edited by
            #5

            hi yczo,
            try hello.toStdString().c_str();
            mind the small letter

            http://doc.qt.io/qt-5/qstring.html#toStdString

            1 Reply Last reply
            0
            • Y Offline
              Y Offline
              yczo
              wrote on 10 May 2016, 13:34 last edited by yczo 5 Oct 2016, 13:43
              #6

              but... there is not a way to direct obtain a char array instead of a string? the question was over an char array hehee ;-)

              char *cHola =

              Thank you very much (I appreciate the kindness)...

              M 1 Reply Last reply 10 May 2016, 13:54
              0
              • S Offline
                S Offline
                sneubert
                wrote on 10 May 2016, 13:53 last edited by
                #7

                you are right, the pointer you get is const, but you can do something like

                char cHola[512] = {0};
                std::copy(hello.toStdString().begin(),hello.toStdString().end(),cHola);
                

                but remind that there´s no boundary check on cHola and you get nonsens if it´s not a basic_string<char>

                1 Reply Last reply
                1
                • Y yczo
                  10 May 2016, 13:34

                  but... there is not a way to direct obtain a char array instead of a string? the question was over an char array hehee ;-)

                  char *cHola =

                  Thank you very much (I appreciate the kindness)...

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 10 May 2016, 13:54 last edited by
                  #8

                  @yczo
                  hi
                  Nope
                  QString dont convert directly to char *.

                  But can I ask why u need char * ?

                  QString class can do anything u can do with char * and in better and more safe way.

                  1 Reply Last reply
                  0
                  • Y Offline
                    Y Offline
                    yczo
                    wrote on 10 May 2016, 14:16 last edited by yczo 5 Oct 2016, 14:18
                    #9

                    Hello and thank you; I need "char *hola" or "char hola[maxSize]" for send simply chars to serial port through Windows handler

                    Greetings

                    K 1 Reply Last reply 10 May 2016, 23:20
                    0
                    • S Offline
                      S Offline
                      sneubert
                      wrote on 10 May 2016, 14:24 last edited by sneubert 5 Oct 2016, 14:27
                      #10

                      so the answer is already there, something like
                      WriteFile(yourComHandle, hello.toStdString().c_str(), hello.length(), &dw_bytesWritten, NULL)
                      should do the job

                      1 Reply Last reply
                      1
                      • Y yczo
                        10 May 2016, 14:16

                        Hello and thank you; I need "char *hola" or "char hola[maxSize]" for send simply chars to serial port through Windows handler

                        Greetings

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 10 May 2016, 23:20 last edited by kshegunov 5 Oct 2016, 23:23
                        #11

                        @yczo said:

                        I need "char *hola" or "char hola[maxSize]" for send simply chars to serial port through Windows handler

                        You're better of using QSerialPort (which accepts QByteArray) and respecting the string's encoding. Also referencing temporaries can be inconspicuously dangerous in some cases. Consider this (modified from @sneubert's example):

                        char * string = hello.toStdString().c_str(); // Now we get a dangling pointer to a location we don't own!
                        WriteFile(yourComHandle, string, hello.length(), &dw_bytesWritten, NULL); // Might work, depending on the compiler, might get a segfault
                        

                        How about this, doesn't it look better:

                        QSerialPort port;
                        // ... init and so on ...
                        
                        QByteArray data = hello.toLatin1(); // Enforce latin1 encoding (or utf8, or local 8-bit or whatever your device is expecting)
                        port.write(data);
                        port.waitForBytesWritten(1000); // Only for illustration, you can (and should) use the async API
                        // ... and so on ...
                        

                        Or, if you really insist on using char *, then enforce the encoding to a byte-array and then get the char pointer from there:

                        QByteArray string = hello.toLatin1();
                        char * strdata = string.data(); // This pointer is valid during the lifetime of the QByteArray instance (provided the byte array is not changed in the meantime)
                        

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        2

                        4/11

                        10 May 2016, 13:24

                        topic:navigator.unread, 7
                        • Login

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