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

[SOLVED] QString to const char *

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 4.3k 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.
  • G Offline
    G Offline
    great88
    wrote on last edited by
    #1

    my code is like this :

    @
    QLineEdit textbox;
    textBox.setText("QWERTY");
    const char * mytext= textBox.text().toUtf8().constData();

    QString mystring = QString::fromUtf8(mytext);
    @

    now mystring just equals "Q" .

    Should it not be "QWERTY" ?

    What am I doing wrong ?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      great88
      wrote on last edited by
      #2

      More to the point , I am using a 3rd party function that takes const char * as argument .

      I only have the function interface not the implementation .

      When I try to get a QString from the QLineEdit and convert it to const char * and feed it to the 3rd party function , the function fails to give proper output .
      When I just feed the equivalent string literal to the function , the function gives correct output .

      So something is wrong with the .toUtf8().constData() conversion from QString to const char * .
      I think .

      1 Reply Last reply
      0
      • M Offline
        M Offline
        MuldeR
        wrote on last edited by
        #3

        I think this code of your's is flawed:
        @const char * mytext = textBox.text().toUtf8().constData();@

        The toUtf8() function returns a QByteArray object containing the UTF8-converted string. And constData() returns a pointer to that QByteArray's internal buffer. But you don't keep the QByteArray object anywhere! So the temporary QByteArray is destroyed instantly - thus your pointer is dangling!

        Try instead one of those:
        @// --- Method #1 ---
        const QByteArray byteArray = mytext = textBox.text().toUtf8();
        const char *mytext = byteArray.constData();

        // --- Method #2 ---
        const char *mytext = strdup(textBox.text().toUtf8().constData());@

        Don't forget free(mytest), if you use method #2 ;-)

        --

        From the docs:
        [quote]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 unless the QByteArray object was created from raw data. The pointer remains valid as long as the byte array isn't reallocated or destroyed.[/quote]

        My OpenSource software at: http://muldersoft.com/

        Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

        Go visit the coop: http://youtu.be/Jay...

        1 Reply Last reply
        0
        • G Offline
          G Offline
          great88
          wrote on last edited by
          #4

          Thank you for explaining that . I tried your method 1 and it works .

          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