PDA

View Full Version : NUll character output for combobox index



mdskpr778
5th October 2008, 13:21
Whenever I run this bit of code it prints blank characters:


void Photo::apertureChanged()
{

QString x;
x = aperture.itemText(0);
QByteArray i = x.toLatin1();
const char *c_str2 = i.data();
printf(c_str2);

}

when really its supposed to print the aperture number of combobox index 0.
it compiles fine.
Thanks in Advance

Junior
5th October 2008, 14:27
mdskpr778,

try:


void Photo::apertureChanged()
{
QString x;
x = aperture.itemText(0);
if( !x.isEmpty() ){
const char *c_str2 = x.toLatin1().data();
printf(c_str2);
} else {
printf( "x is empty\n" );
}
}

spud
8th October 2008, 18:22
These two lines will most likely crash your application:

const char *c_str2 = x.toLatin1().data();
printf(c_str2);

Use:

printf(x.toLatin1().constData());
That has to be the single most common Qt mistake.;)