PDA

View Full Version : QString and MS Visual Studio 2005



ToddAtWSU
18th June 2007, 15:24
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


std::string sname = selected.toStdString( );

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!

high_flyer
18th June 2007, 15:52
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)...

^NyAw^
18th June 2007, 16:28
Hi,

Try: "YourQString.toAscii().data()"

ToddAtWSU
18th June 2007, 17:01
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.


/************************
SLOTS
************************/
void OpenFileDialog::populateProperties( const QString selected )
{
std::string sname = selected.toStdString( );
//char *filename = strdup( sname.c_str( ) );
}

Here is the connection statement inside my constructor

connect( this, SIGNAL( currentChanged( const QString ) ), this, SLOT( populateProperties( const QString ) ) );

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

high_flyer
18th June 2007, 17:17
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...

ToddAtWSU
18th June 2007, 17:23
Taking out the const still produces the same results.

ToddAtWSU
18th June 2007, 17:26
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.

high_flyer
18th June 2007, 17:34
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:


void OpenFileDialog::populateProperties( const QString selected )
{
qDebug()<<selected;
//std::string sname = selected.toStdString( );
//char *filename = strdup( sname.c_str( ) );
}

ToddAtWSU
18th June 2007, 17:41
void OpenFileDialog::populateProperties( const QString selected )
{
qDebug()<<selected;
//std::string sname = selected.toStdString( );
//char *filename = strdup( sname.c_str( ) );
}

It prints out the filename I have selected, which is what I would expect it to print.

ToddAtWSU
18th June 2007, 19:49
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.

QString str = QString( "Hello World" );
populateProperties( str );
This code works fine as does

populateProperties( QString( "Hello World" ) );
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::populateProperties( 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.

jesseelliott
18th June 2007, 21:07
Have you tried passing "const QString&" in the signal instead of "const QString"?

ToddAtWSU
18th June 2007, 21:23
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.

ChristianEhrlicher
19th June 2007, 06:42
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 ;)

high_flyer
19th June 2007, 09:19
Taking out the const still produces the same results.
Did you do this both on signal AND slot?

ToddAtWSU
19th June 2007, 13:35
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!

ChristianEhrlicher
19th June 2007, 13:41
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.

ToddAtWSU
19th June 2007, 17:28
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.