Results 1 to 14 of 14

Thread: toStdString is returning an empty string !!!!

  1. #1
    Join Date
    Oct 2010
    Posts
    95
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question toStdString is returning an empty string !!!!

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    Did you make sure the input string is not empty in the first place? if so how?
    Post your code.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2010
    Posts
    95
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    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();

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    can you show the following several lines too?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Oct 2010
    Posts
    95
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    Thanks,

    I have this problem since several months !

    Here is the complete method :

    Qt Code:
    1. void psMainWindow::on_Menu_Save()
    2. {
    3. if (_currentFilePath.length() < 1)
    4. {
    5. on_Menu_SaveAs();
    6. return;
    7. }
    8.  
    9. UpdatePanelWidget::Start();
    10.  
    11. pureGraph::IE::Exporter exporter;
    12. string file = _currentFilePath.toStdString();
    13. exporter.Export(GlobalContext::Scene, file);
    14.  
    15. UpdatePanelWidget::Stop();
    16. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    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?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Oct 2010
    Posts
    95
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    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...tring-heap-bug

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

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    Try to clean and rebuild the project/solution.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Oct 2010
    Posts
    95
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    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 !!!

  10. #10
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    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??
    A camel can go 14 days without drink,
    I can't!!!

  11. #11
    Join Date
    Oct 2010
    Posts
    95
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    Yes, it is a simple file name !!!

  12. #12
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    Quote Originally Posted by pl01 View Post
    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??
    A camel can go 14 days without drink,
    I can't!!!

  13. #13
    Join Date
    Oct 2010
    Posts
    95
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: toStdString is returning an empty string !!!!

    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 ??

  14. #14
    Join Date
    Nov 2010
    Posts
    2
    Qt products
    Qt4

    Default Re: toStdString is returning an empty string !!!!

    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:

    Qt Code:
    1. QString txt = "Some text";
    2. std::string txt_str = txt.toStdString();
    3. const char* txt_str_chr = txt_str.c_str(); // works, content is "Some text"
    4. const char* txt_str_chr_one_line = txt.toStdString().c_str(); // empty!! vc-bug??
    To copy to clipboard, switch view to plain text mode 

    I tried this in VS 2013

Similar Threads

  1. QLineEdit empty string
    By zgulser in forum Newbie
    Replies: 4
    Last Post: 3rd August 2010, 14:18
  2. QString::toStdString in PyQt Question
    By di_zou in forum Newbie
    Replies: 1
    Last Post: 2nd November 2009, 20:09
  3. QSplitter::saveState returns empty string
    By miraks in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2008, 21:30
  4. returning a string
    By mickey in forum General Programming
    Replies: 2
    Last Post: 3rd February 2008, 19:41
  5. QString .toStdString heap bug
    By bpetty in forum Qt Programming
    Replies: 1
    Last Post: 3rd October 2006, 17:10

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.