PDA

View Full Version : toStdString is returning an empty string !!!!



pl01
18th April 2011, 15:01
Hi,

Often, but not each time, "toStdString" is converting the string into an empty string !! It is completely random and do not know how to solve this problem ?

do you have any idea ?

Thanks

high_flyer
18th April 2011, 15:04
Did you make sure the input string is not empty in the first place? if so how?
Post your code.

pl01
18th April 2011, 15:07
Yes, I'm in the debugger and the debugger show me the content of my string... everything sounds fine !!!
Here is the code... so simple !!!

string file = _currentFilePath.toStdString();

high_flyer
18th April 2011, 15:22
can you show the following several lines too?

pl01
18th April 2011, 15:25
Thanks,

I have this problem since several months !

Here is the complete method :



void psMainWindow::on_Menu_Save()
{
if (_currentFilePath.length() < 1)
{
on_Menu_SaveAs();
return;
}

UpdatePanelWidget::Start();

pureGraph::IE::Exporter exporter;
string file = _currentFilePath.toStdString();
exporter.Export(GlobalContext::Scene, file);

UpdatePanelWidget::Stop();
}

high_flyer
18th April 2011, 15:27
and you say that when you stand on line 13 in the debugger, '_currentFilePath' has a valid string in it, and 'file' is an empty string?

pl01
18th April 2011, 15:30
I work with Visual Studio 2008 (for info) and I've also seen this post but not sure it is related : http://www.qtcentre.org/threads/3875-QString-.toStdString-heap-bug

Yes, on line 13... '_currentFilePath' is valid but 'file' is empty !

high_flyer
18th April 2011, 15:35
Try to clean and rebuild the project/solution.

pl01
21st April 2011, 13:59
Thanks,

I have try on several machines... I have this bug since several month now !!!! I have no idea on how I can fix this !
Is there another way to convert a QString to a std::string ?

I have try : qPrintable(_currentFilePath)

But still have the same error !!!

mcosta
21st April 2011, 15:51
std::string QString::toStdString () const
...
If the QString contains Unicode characters that the QTextCodec::codecForCStrings() codec cannot handle, using this operator can lead to loss of information.
...


Are you sure your QString doesn't contain Unicode characters not handled??

pl01
21st April 2011, 15:57
Yes, it is a simple file name !!!

mcosta
21st April 2011, 16:18
Yes, it is a simple file name !!!

This isn't a warranty that you don't have any "strange character".

What is the language of your system?

Can you try with QString::toStdWString??

pl01
29th April 2011, 09:42
Thanks,

I have try with :

std::wstring wFileName = _currentFilePath.toStdWString();
string fileName1 = _currentFilePath.toStdString(); // BUG
string fileName2 = qPrintable(_currentFilePath);

But all theses one are empty !! But not _currentFilePath ??

username2010
27th August 2014, 11:06
I had the same problem. It does not seem to be a bug in Qt, but one in the VC Compiler.
If you write toStdString().c_str() in one line, the result is empty.
If you write it in two lines, the result is correct:



QString txt = "Some text";
std::string txt_str = txt.toStdString();
const char* txt_str_chr = txt_str.c_str(); // works, content is "Some text"
const char* txt_str_chr_one_line = txt.toStdString().c_str(); // empty!! vc-bug??


I tried this in VS 2013