Results 1 to 8 of 8

Thread: QString to char

  1. #1
    Join Date
    Jun 2006
    Posts
    34
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QString to char

    Is their any way I can convert QString to char ??

    Thanks in advance,
    Boss

  2. #2
    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: QString to char

    You can't convert whole string to a single character. What exactly are you trying to do?

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QString to char

    something like that should work ...

    QString str = "Qt4 rocks!";
    const char* ch_str = str.toStdString().c_str();

    std::cout << "converting QStrings to chars: " << ch_str << std::endl;

  4. #4
    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: QString to char

    Quote Originally Posted by momesana
    something like that should work ...

    QString str = "Qt4 rocks!";
    const char* ch_str = str.toStdString().c_str();

    std::cout << "converting QStrings to chars: " << ch_str << std::endl;
    Uuuuu.... this is a snippet which can cause a beautiful segmentation fault.

    std::string here will be a temporary object, so it'll get invalid as soon as c_str() returns, causing an invalid pointer to be assigned to ch_str and a potential memory access error to occur when you try to access it at some later point in time.

    Try this instead:
    Qt Code:
    1. QString str = "Qt4 rocks!";
    2. char * ch_str = qstrdup(qPrintable(str));
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Smile Re: QString to char

    Quote Originally Posted by wysota
    Uuuuu.... this is a snippet which can cause a beautiful segmentation fault.
    Well, I don't think so!
    I am not an experienced C++ programmer but I doubt that the code above will cause any problems.
    Try this

    Qt Code:
    1. const char * ch_str;
    2. { // Block of code
    3. QString str = "Qt4 rocks!";
    4. ch_str = str.toStdString().c_str();
    5. } // End of block of code. All local variables in the block cease to exist
    6. std::cout << ch_str << std::endl;
    7. // std::cout << str.toStdString() << std::endl; // That would cause an error
    To copy to clipboard, switch view to plain text mode 
    Printing str.toStdString() would cause an error since the variable went out of scope and died, the value it refered to however, still exists (as proven by the above print statement). Correct me if I am wrong. As I said, I am not a C++ expert .

    Thanx in advance
    momesana
    Last edited by momesana; 20th July 2006 at 12:37.

  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: QString to char

    Quote Originally Posted by momesana
    as proven by the above print statement
    It doesn't work --- it "works". Because memory where ch_str points to wasn't overwritten yet, you can read it, but it will crash someday.

  7. #7
    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: QString to char

    Quote Originally Posted by momesana
    ...(as proven by the above print statement)...
    It's not a proof, it's plain luck. The fact that something works once doesn't mean it works. The fact that something doesn't work once means it doesn't work. If you assign the value as you did and then allocate all the memory available in your system, then set it all to zeroes, deallocate and then try to print that variable, you'll get nothing. Furthermore it's enough to test your example code using some memory debugger like Valgrind, I'm sure it'll report a warning in the line where you try to access the variable.

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QString to char

    seems like I have a lot of code to rewrite then

Similar Threads

  1. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  2. Opening swf file in the default browser
    By munna in forum Qt Programming
    Replies: 16
    Last Post: 5th May 2006, 09:33
  3. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10
  4. dialog box
    By Bahar in forum Qt Programming
    Replies: 3
    Last Post: 31st January 2006, 14:52
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

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.