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
Code:
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.
Code:
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 . it is not showing any value on . So then i used
Code:
const char* test= lineEdit->text().toLatin1().data();
this above line is now showing me everything perfectly on VS2010.
I have no idea why
Code:
const char* test= lineEdit->text().toStdString().c_str();
is working perfectly on and why not on . Can anyone please guide me where i m doing wrong. Thanks
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,
_