Qt5 convert QString into char [] or char *



  • 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



  • @yczo hello.ToStdString().c_str()



  • 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'


  • Moderators

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



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

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



  • 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)...



  • 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>


  • Qt Champions 2016

    @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.



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

    Greetings



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


  • Qt Champions 2016

    @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)
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.