Results 1 to 17 of 17

Thread: QString and MS Visual Studio 2005

  1. #1
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Question QString and MS Visual Studio 2005

    I am using Qt 4.3.0 with MS Visual Studio 2005 and I am having major problems trying to convert a QString to a char*. I get a Debug Assertion Failed! message when I try to create a std::string from a QString. I am used to using QString.toStdString( ).c_str( ) to convert my QString to a char* and I was having problems with this when I tried to exit the function I was in. I broke my call into 2 lines and the only line I am calling looks like this

    Qt Code:
    1. std::string sname = selected.toStdString( );
    To copy to clipboard, switch view to plain text mode 

    This is the only call inside the function, I have commented the rest out and I still crash trying to exit the function. Has anybody else had problems trying to convert a QString to a std::string or char* in MS Visual Studio 2005? I don't recall having this problem when I used Visual Studio 2003. Thanks for your help!

  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: QString and MS Visual Studio 2005

    This is the only call inside the function
    Can you post the full function then?
    'selected' must also be defined in that function (or higher scope)...
    ==========================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
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString and MS Visual Studio 2005

    Hi,

    Try: "YourQString.toAscii().data()"
    Òscar Llarch i Galán

  4. The following user says thank you to ^NyAw^ for this useful post:

    ToddAtWSU (19th June 2007)

  5. #4
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Smile Re: QString and MS Visual Studio 2005

    Here is the function. I deleted all of the rest of the commented code so you didn't have to see all my comments. It crashes when it tries to exit this function.

    Qt Code:
    1. /************************
    2.   SLOTS
    3.  ************************/
    4. void OpenFileDialog::populateProperties( const QString selected )
    5. {
    6. std::string sname = selected.toStdString( );
    7. //char *filename = strdup( sname.c_str( ) );
    8. }
    To copy to clipboard, switch view to plain text mode 

    Here is the connection statement inside my constructor
    Qt Code:
    1. connect( this, SIGNAL( currentChanged( const QString ) ), this, SLOT( populateProperties( const QString ) ) );
    To copy to clipboard, switch view to plain text mode 

    As far as the toAscii( ) call I will try that out here soon. Thanks for your help!

  6. #5
    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: QString and MS Visual Studio 2005

    I am not sure about this but I think the 'const' is the prblem.
    Try removing 'const' or make it a 'const' reference.
    The logic I am using is, the const is not allowed to be modified, and deleting the object (at end of function scope) might be seen as modification...
    ==========================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. #6
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString and MS Visual Studio 2005

    Taking out the const still produces the same results.

  8. #7
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Red face Re: QString and MS Visual Studio 2005

    Quote Originally Posted by ^NyAw^ View Post
    Try: "YourQString.toAscii().data()"
    When I try this unfortunately, I print out the char* that is supposed to be returned and all I see is garbage instead of the path to the file I chose. So this does not seem to work either. But I do not crash when I do run this code instead of going from a QString to a std::string to a char*. So it makes me wonder if there is an issues with std::string.

  9. #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: QString and MS Visual Studio 2005

    When I try this unfortunately, I print out the char* that is supposed to be returned and all I see is garbage instead of the path to the file I chose. So this does not seem to work either.
    what do you get when you try:
    Qt Code:
    1. void OpenFileDialog::populateProperties( const QString selected )
    2. {
    3. qDebug()<<selected;
    4. //std::string sname = selected.toStdString( );
    5. //char *filename = strdup( sname.c_str( ) );
    6. }
    To copy to clipboard, switch view to plain text mode 
    ==========================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.

  10. #9
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Unhappy Re: QString and MS Visual Studio 2005

    Quote Originally Posted by high_flyer View Post
    Qt Code:
    1. void OpenFileDialog::populateProperties( const QString selected )
    2. {
    3. qDebug()<<selected;
    4. //std::string sname = selected.toStdString( );
    5. //char *filename = strdup( sname.c_str( ) );
    6. }
    To copy to clipboard, switch view to plain text mode 
    It prints out the filename I have selected, which is what I would expect it to print.

  11. #10
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Red face Re: QString and MS Visual Studio 2005

    I think I have definately isolated where the problem occurs. The problem seems to occur when the Signal passes the QString to the Slot. I have been trying little tweaks to the code and finally in my constructor commented out my connect statement and instead created a local QString and then called the Slot like it was a function with passing in the local QString.
    Qt Code:
    1. QString str = QString( "Hello World" );
    2. populateProperties( str );
    To copy to clipboard, switch view to plain text mode 
    This code works fine as does
    Qt Code:
    1. populateProperties( QString( "Hello World" ) );
    To copy to clipboard, switch view to plain text mode 
    But if I take out these direct calls and uncomment the connect statement, I get crashes 100% of the time. Right now all I am attempting to do inside the code is this:

    void OpenFileDialog:opulateProperties( const QString selected )
    {
    qDebug( ) << selected;
    char *filename = strdup( selected.toStdString( ).c_str( ) );
    fprintf( stderr, "filename = %s\n", filename );
    }

    The QString always prints correctly but anytime I try to use the converted string or leave the function, I get the same heap pointer error. So it seems to be something when I pass in a QString through the SIGNAL to the SLOT. Thanks for your help in trying to help me fix this bug.

  12. #11
    Join Date
    Jun 2007
    Location
    Colorado Springs, CO - USA
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString and MS Visual Studio 2005

    Have you tried passing "const QString&" in the signal instead of "const QString"?

  13. #12
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Unhappy Re: QString and MS Visual Studio 2005

    Quote Originally Posted by jesseelliott View Post
    Have you tried passing "const QString&" in the signal instead of "const QString"?
    Adding the & didn't seem to help me either. Thanks for the idea though.

  14. #13
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString and MS Visual Studio 2005

    You're mixing debug Qt libs with release Application (or the other way round). You must not do this with msvc!
    Also I don't see why you need to convert QString to std:string at all

  15. #14
    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: QString and MS Visual Studio 2005

    Taking out the const still produces the same results.
    Did you do this both on signal AND slot?
    ==========================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.

  16. #15
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Cool Re: QString and MS Visual Studio 2005

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    Try: "YourQString.toAscii().data()"
    I decided to try this again this morning and I got it working! I just had to put it inside a strdup( ) so the char* would be allocated. Thanks for your help but it seems to me VS 2005 has some problems with strings and char* manipulation. I am getting problems just trying to turn a std::string to a char*. Thanks for all your help!

  17. #16
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString and MS Visual Studio 2005

    It's funny to talk against a wall, therefore I reply myself... you are mixing Qt debug and release libs. You must not do this when using msvc.

  18. #17
    Join Date
    Jan 2006
    Location
    Ohio
    Posts
    332
    Thanks
    37
    Thanked 8 Times in 4 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QString and MS Visual Studio 2005

    You say I am mixing but you don't say how you know I am mixing. Since I don't know this, how do I know I am mixing debug and release libraries? Thanks.

Similar Threads

  1. QT with visual c++ 2005
    By v3n0w in forum Installation and Deployment
    Replies: 5
    Last Post: 20th May 2007, 12:37
  2. QString in Visual Studio 2005 has compile error
    By KaKa in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2007, 10:05
  3. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  4. Can't compile programs in Visual Studio.net 2005
    By xgoan in forum Qt Programming
    Replies: 3
    Last Post: 7th July 2006, 14: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
  •  
Qt is a trademark of The Qt Company.