Results 1 to 11 of 11

Thread: Converting QString to char* in onl line

  1. #1
    Join Date
    Dec 2009
    Posts
    62
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Converting QString to char* in onl line

    I have a QString that I need to pass as qrgument to a C dll wanting a char*.

    When I do like this:
    char* str_p = myQString.toAscii().constData();
    qDebug("%s", str_p );

    It prints ???????? all over the screen so the pointer is crap.

    However if I do like this:
    QByteArray ba = myQString.toAscii();
    char* str_p = ba.constData();
    qDebug("%s", str_p);

    It works just fine.

    I really would like to have the QString to char* conversion as a one liner, why does the first approach fail?

  2. #2
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Converting QString to char* in onl line

    The first version fails because you get a pointer to a temporary variable that is forgotten by the time you use your pointer. In the second case, the QByteArray is still around.

  3. #3
    Join Date
    Dec 2009
    Posts
    62
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting QString to char* in onl line

    thanks drhex

    So can I assume that there is no oneliner to achive this?

  4. #4
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Converting QString to char* in onl line

    Qt Code:
    1. char* str_p = myQString.toAscii().constData();
    2. qDebug("%s", str_p );
    To copy to clipboard, switch view to plain text mode 

    when I try to compile your code, I get:
    C:/dev/testproj/mainwindow.cpp:11: error: invalid conversion from `const char*' to `char*'

    instead, do the following:
    Qt Code:
    1. const char* str_p = myQString.toAscii().constData();
    2. qDebug("%s", str_p );
    To copy to clipboard, switch view to plain text mode 

    This gives me the correct string.

  5. #5
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Converting QString to char* in onl line

    Quote Originally Posted by drhex View Post
    The first version fails because you get a pointer to a temporary variable that is forgotten by the time you use your pointer. In the second case, the QByteArray is still around.
    Is this really true?

    myString.toAscii() will return a QByteArray and then myString.toAscii.constData() will return a const char*.

    from Qt Documentation:


    const char * QByteArray::constData () const

    Returns a pointer to the data stored in the byte array. The pointer can be used to access the bytes that compose the array. The data is '\0'-terminated. The pointer remains valid as long as the byte array isn't reallocated or destroyed.

    This function is mostly useful to pass a byte array to a function that accepts a const char *.

    Note: A QByteArray can store any byte values including '\0's, but most functions that take char * arguments assume that the data ends at the first '\0' they encounter.

    See also data() and operator[]().

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting QString to char* in onl line

    It's true. QString's toAscii() returns a QByteArray. That QByteArray will be destroyed by the time the char * pointer is assigned str_p, unless you store it somewhere (as in the second version). The contents may or may not be what your expecting because of this, or it may just crash your program.

    The Qt documentation talks about the const_data that QByteArray returns, which is also true, but that pointer is no good if you destroy the QByteArray holding that pointer.

  7. #7
    Join Date
    Dec 2009
    Posts
    62
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting QString to char* in onl line

    Thanks for all the replies, seems like the simple question spotted a slightly tricky problem.

    My assumption is simply that I will not be able to convert my QStrings into char* inside the function call to the dll that expects a char*, instead I have to do the intermediate step to convert to ByteArray for all variables.

    //Impossible
    void myQTFunc(QString str1)
    {
    myDllfunc(str1.toAscii().constData());
    }

    //Possible
    void myQTFunc(QString str1)
    {
    QByteArray ba1 = str1.toAscii();
    myDllfunc(ba1.constData);
    }

    So does anyone know if the conversion from QString to QByte array involves any copying? The QString is Unicode right? Does that mean that the toAscii() conversion creates a local copy of the string contents as a C char array?

    I also assume that the constData() just returns the internal char* from QByteArray and that it does not involve any copying of data, does this sound correct?

  8. #8
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Converting QString to char* in onl line

    when you use the QByteArray::constData() method, a shallow copy takes place, which is very fast, i.e. it is just getting a pointer and incrementing a reference count. QByteArray::data() is a deep copy, which is a real copy and thus slower.

    Just read the documentation on QByteArray::constData() - it is explained very well.

  9. #9
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting QString to char* in onl line

    What about using this directly as parameter to your call?

    Qt Code:
    1. str.toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 

    HIH

    Johannes

  10. #10
    Join Date
    Dec 2009
    Posts
    62
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Converting QString to char* in onl line

    Ok this seemed to work, nice.

    However the doc says that:
    This operator is only available if Qt is configured with STL compatibility enabled.

    Is this always the default setting? I need this to run on Win32, Windows Mobile and S60. Can I assume that the STL will always be supported.

  11. #11
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Converting QString to char* in onl line

    How are you going to use a c dll on all those platforms?

    And I have no idea, if STL is available for those platforms. Sorry!

    Johannes

Similar Threads

  1. File rename detection
    By bunjee in forum Qt Programming
    Replies: 6
    Last Post: 23rd July 2009, 15:22
  2. QTcpSocket exception.
    By Fastman in forum Qt Programming
    Replies: 9
    Last Post: 29th January 2008, 13:51
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  5. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.