Results 1 to 2 of 2

Thread: Difference between Qt Strings Functions

  1. #1
    Join Date
    Jul 2013
    Posts
    16
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Difference between Qt Strings Functions

    I made a project on VS2005 using Qt -4.7.4 version. In that project i used lineEdit and took the text from lineEdit like this

    Qt Code:
    1. const char* test= lineEdit->text().toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 

    The above line worked perfectly but now i am using VS2010 and using the same above line but it is not showing me anything for the test variable.

    Qt Code:
    1. qDebug()<<lineEdit->text()<<endl;
    To copy to clipboard, switch view to plain text mode 

    The above line is printing the right value of lineEdit but when i am using the `std::string` and then using
    cout
    . it is not showing any value on
    VS2010
    . So then i used

    Qt Code:
    1. const char* test= lineEdit->text().toLatin1().data();
    To copy to clipboard, switch view to plain text mode 

    this above line is now showing me everything perfectly on VS2010.

    I have no idea why
    Qt Code:
    1. const char* test= lineEdit->text().toStdString().c_str();
    To copy to clipboard, switch view to plain text mode 
    is working perfectly on
    VS2005
    and why not on
    VS2010
    . Can anyone please guide me where i m doing wrong. Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Difference between Qt Strings Functions

    Either method creates a dangling pointer since the object owning the data is destroyed right after the assignment.

    If you want to work with the C array, either copy the result of the c_str() or data() methods or keep the owning object (the std::string or QByteArray respectively)

    Cheers,
    _

Similar Threads

  1. Unicode strings int Qt 4.2.1
    By mkrentovskiy in forum Qt Programming
    Replies: 12
    Last Post: 29th December 2011, 10:02
  2. How to add multiple strings to a spinner
    By athulms in forum Newbie
    Replies: 4
    Last Post: 28th July 2011, 12:46
  3. How to add multiple strings to a spinner
    By athulms in forum Qt Quick
    Replies: 0
    Last Post: 28th July 2011, 08:36
  4. how to add multiple strings into the spinner
    By vinayaka in forum Qt Quick
    Replies: 3
    Last Post: 27th July 2011, 12:55
  5. Replies: 1
    Last Post: 21st September 2009, 08:30

Tags for this Thread

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.