PDA

View Full Version : Difference between Qt Strings Functions



saad_saadi
27th May 2014, 10:51
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


const char* test= lineEdit->text().toStdString().c_str();

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.


qDebug()<<lineEdit->text()<<endl;

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


const char* test= lineEdit->text().toLatin1().data();

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

I have no idea why
const char* test= lineEdit->text().toStdString().c_str(); is working perfectly on
VS2005 and why not on
VS2010. Can anyone please guide me where i m doing wrong. Thanks

anda_skoa
27th May 2014, 17:49
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,
_