Results 1 to 17 of 17

Thread: char* from QString

  1. #1
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default char* from QString

    Hello,
    Another really newbie question. What is the appropriate way to get a char * pointer from a QString object? I'm using object.toLocal8Bit().constData(), but doesn't always work (or maybe it doesn't work at all).
    - BC

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: char* from QString

    It works, just remember it is a const char*. BTW. there is a macro which is a shortcut to the above mentioned combination:
    Qt Code:
    1. QString str = "xxxx";
    2. printf("%s", qPrintable(str));
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* from QString

    Please, search the forum before you ask a question. This topic was already discussed here: http://www.qtcentre.org/forum/f-qt-p...ting-3211.html

  4. #4
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: char* from QString

    Quote Originally Posted by jacek View Post
    Please, search the forum before you ask a question. This topic was already discussed here: http://www.qtcentre.org/forum/f-qt-p...ting-3211.html
    So, will the qPrintable work, or is the only (best) way to create a QByteArray that doesn't go out of scope? I looked at the previous post and can I assume yours is the only way to do this?

    Incidentally, I find the Qt site difficult to search. This forum is excellent, however!
    - BC

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: char* from QString

    Quote Originally Posted by bruccutler View Post
    Incidentally, I find the Qt site difficult to search.
    Have you tried Qt Assistant's indexing and searching facilities? In my opinion it's superior..
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* from QString

    Quote Originally Posted by bruccutler View Post
    So, will the qPrintable work, or is the only (best) way to create a QByteArray that doesn't go out of scope? I looked at the previous post and can I assume yours is the only way to do this?
    qPrintable() will only work as a function parameter, so either use:
    Qt Code:
    1. char *cstr = qstrdup( qPrintable( str ) );
    2. ...
    3. delete [] cstr;
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QByteArray ba( str.toLocal8bit() );
    2. const char *cstr = ba.constData(); // cstr is valid as long as ba is alive
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2007
    Posts
    91
    Thanks
    21
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Red face Re: char* from QString

    Thanks. I've gone with the second method. Thanks for the help and I apologize that I didn't do a search first.

  8. #8
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* from QString

    From the Faq

    You can use toLatin1() to get a byte array and then call data() on that one which will give you a char*, i.e
    toLatin1().data() // Is this wrong ??

    http://doc.trolltech.com/4.2/qstring.html#toLatin1

    To convert a QString to a char* you can do the following:
    QString(QLatin1String(charstringhere))

    See the documentation:

    http://doc.trolltech.com/4.2/qlatin1string.html
    So is the Faq wrong ??

    I have tried this program
    Qt Code:
    1. #include <iostream>
    2. #include <QString>
    3.  
    4. int main(){
    5. QString str = "Sunil Thaha";
    6. std::cout << str.toLatin1().data() << std::endl;
    7.  
    8. const char *charPointer = str.toLatin1().data();
    9. std::cout << charPointer << std::endl;
    10.  
    11. if( charPointer == str.toLatin1().data() ){
    12. std::cout<< "Still pointing to same location ";
    13. }
    14. return 0;
    15. }
    To copy to clipboard, switch view to plain text mode 

    My results confirms with the Faq... I am able to get a const pointer to the data() and further calls returns the same memory address ?? Both Wiki and jaceks post discourages us from using this. So who is right here ?
    Last edited by sunil.thaha; 1st February 2007 at 09:17.
    We can't solve problems by using the same kind of thinking we used when we created them

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: char* from QString

    Qt Code:
    1. str.toLatin1().data()
    To copy to clipboard, switch view to plain text mode 
    This creates a temporary object (QByteArray) and returns a pointer to its data, but the temporary object is deleted immediately. The memory is not cleared, the compiler does some optimisations, etc. - the bottom line is the data IS there, but only because you're lucky. If you try to use the pointer after some time the byte array is deleted, you'll eventually receive trash which may cause your app to malfunction. This is strictly related to memory management on your system and to the compiler used. It is not safe to use such constructions in terms of the C++ standard.

    You can think of it as the same as if you'd send a signal accross threads in Qt3 - basically it works (unfortunately only "most of the time"), but it doesn't mean that the code is correct.

  10. The following user says thank you to wysota for this useful post:

    stevey (14th February 2007)

  11. #10
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* from QString

    Please refer to this Faq http://www.trolltech.com/developer/k...30.9032238253/

    How can we ask for correction ?
    We can't solve problems by using the same kind of thinking we used when we created them

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: char* from QString

    There is nothing wrong in that entry, besides it seems to be an internal Trolltech tool and I'm sure they know about the quirk of storing the volatile pointer.

  13. #12
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* from QString

    Quote Originally Posted by wysota View Post
    There is nothing wrong in that entry,
    But all the posts above are against it isn't it ? And that being an official Faq; should not be pointing in a wrong direction
    We can't solve problems by using the same kind of thinking we used when we created them

  14. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: char* from QString

    Quote Originally Posted by sunil.thaha View Post
    But all the posts above are against it isn't it ?
    No, it doesn't say anything about storing the data pointer and only referring to it after the temporary object is destroyed is in violation with the specification. This is a perfectly legitimate statement:

    Qt Code:
    1. QString str = "xxxx";
    2. printf("%s\n", str.toLocal8Bit().data());
    To copy to clipboard, switch view to plain text mode 

    whereas this one is not:

    Qt Code:
    1. QString str = "xxxx";
    2. char *ptr = str.toLocal8Bit().data();
    3. printf("%s\n", ptr);
    To copy to clipboard, switch view to plain text mode 

    If you want to store the pointer, keep the byte array intact:
    Qt Code:
    1. QString str = "xxxx";
    2. QByteArray ba = str.toLocal8Bit();
    3. char *ptr = ba.data(); // this is valid as long as "ba" is
    4. //...
    5. printf("%s\n", ptr);
    To copy to clipboard, switch view to plain text mode 

  15. The following user says thank you to wysota for this useful post:

    sunil.thaha (1st February 2007)

  16. #14
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* from QString

    Waw !!

    Beautiful answer... Thanks a lot for the time and patience.
    Long live QtCentre
    We can't solve problems by using the same kind of thinking we used when we created them

  17. #15
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* from QString

    After this discussions and the subsequent discussion with Qt-Interest mailing list. I had requested the change told above. Trolltech has accepted the change request and has modified the faq

    Here is the link to that

    http://www.trolltech.com/developer/k...-30.9032238253


    Thanks once again.
    We can't solve problems by using the same kind of thinking we used when we created them

  18. The following user says thank you to sunil.thaha for this useful post:

    bruccutler (13th February 2007)

  19. #16
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: char* from QString

    Btw, jacek wrote delete [] cstr, but you dont need to delete a pointer unless you initialized it with new keyword.

  20. #17
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: char* from QString

    From the docs:
    char * qstrdup ( const char * src ) Returns a duplicate string.
    Allocates space for a copy of src, copies it, and returns a pointer to the copy. If src is 0, it immediately returns 0.
    Ownership is passed to the caller, so the returned string must be deleted using delete[]
    We can't solve problems by using the same kind of thinking we used when we created them

Similar Threads

  1. QString problem
    By vermarajeev in forum Qt Programming
    Replies: 9
    Last Post: 26th October 2006, 19:10
  2. Replies: 2
    Last Post: 12th October 2006, 09:17
  3. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  4. dialog box
    By Bahar in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 14:52
  5. How to create custom slot in Qt Designer 4.1?
    By jamadagni in forum Qt Tools
    Replies: 31
    Last Post: 18th January 2006, 20:46

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.