Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, April 2007
Curious behavior...


Message 1 in thread

I have observed a rather startling bit of behavior that I hope someone will
be able to explain for me.

I have two programs that perform a similar function.  In fact, they are
essentially identical except that one of them is a "console" program (using
stdin/stdout for all communications with the user) and the other is a
QApplication (using GUI windows, dialogs and the like).

Some of the common routines used by these programs write text to stdout
regardless of this.  They no nothing at all about where they are running. 
There's no fooling around.  They use the standard "puts" function to write
to stdout like this:
        QString text;
        puts( qPrintable(text) );

Now it so happens that "text" occasionally contains unicode characters.  And
therein lies the catch:  When I run the console version of the program, the
unicode characters disappear from the output.  That was not a surprise to
me, since there is no ASCII equivalent.

But when I run the GUI version of the program, these same unicode characters
are now displayed in the output!  I'm not talking about unicode characters
appearing in some QWidget; I *expect* that to work.  I'm talking about
unicode character representations appearing in the stdout file!

HUH?!?

--
 [ signature omitted ] 

Message 2 in thread

Larry Bristol wrote:

> I have observed a rather startling bit of behavior that I hope someone
> will be able to explain for me.
> 
> I have two programs that perform a similar function.  In fact, they are
> essentially identical except that one of them is a "console" program
> (using stdin/stdout for all communications with the user) and the other is
> a QApplication (using GUI windows, dialogs and the like).
> 
> Some of the common routines used by these programs write text to stdout
> regardless of this.  They no nothing at all about where they are running.
> There's no fooling around.  They use the standard "puts" function to write
> to stdout like this:
>         QString text;
>         puts( qPrintable(text) );
> 
> Now it so happens that "text" occasionally contains unicode characters. 
> And therein lies the catch:  When I run the console version of the
> program, the unicode characters disappear from the output.  That was not a
> surprise to me, since there is no ASCII equivalent.
> 
> But when I run the GUI version of the program, these same unicode
> characters are now displayed in the output!  I'm not talking about unicode
> characters appearing in some QWidget; I *expect* that to work.  I'm
> talking about unicode character representations appearing in the stdout
> file! 
> 
> HUH?!?

I still cannot explain *why* it works this way, but I have figured out the
difference, and a way to make it work the same in both applications.  The
secret (besides banging the rocks together) seems to result from the
definition of "qPrintable":

        const char* qPrintable ( const QString& str )
        Returns str as a const char *. This is equivalent to
        str.toLocal8bit().constData().

In the console application (which BTW is *not* a QCoreApplication), the
local encoding is apparently ASCII,  In the GUI application, it appears
that the local encoding has been set to UTF-8.  Since I do not have a clue
how to do that, I can promise it wasn't done by me!  :-)

For future ease of use, I would appreciate someone telling me how to change
(or specify) the local encoding.  I'm probably just dense, but everything
I've read in the doc might as well be written in Klingonese for all the
good it does me.  (Does that require a different encoding?)  In the
meantime, I replaced:
        puts( qPrintable(text) );
with:
        puts( text.toUtf8().data() );
and the unicode characters I'm using are now displayed correctly on the
console.

--
 [ signature omitted ] 

Message 3 in thread

Hi,

> I have two programs that perform a similar function.  In fact, they are
> essentially identical except that one of them is a "console" program (using
> stdin/stdout for all communications with the user) and the other is a
> QApplication (using GUI windows, dialogs and the like).

The source code is essentially identical, but what about the build process? 
Are these two programs both built using qmake?

--
 [ signature omitted ] 

Message 4 in thread

Dimitri wrote:

> The source code is essentially identical, but what about the build
> process? Are these two programs both built using qmake?

Yes.  The only significant difference that I can see is that the GUI version
has "CONFIG+=resources" and the CUI version does not.  They both use a set
of functions from a shared library, and all output to stdout is done by
calls to (overloaded) static functions in that library defined like this:

/************************************************************************
 ************************************************************************
 *   log - write text to log                                            *
 ************************************************************************
 ************************************************************************/
void            MyClass::log(const QString& text)
{
//=======================================================================
//      Write the string to stdout
//=======================================================================
  puts( text.toUtf8().constData() );
//=======================================================================
}

/************************************************************************
 ************************************************************************
 *   log - write text to log                                            *
 ************************************************************************
 ************************************************************************/
void            MyClass::log(const char* text)
{
//=======================================================================
//      Write the string to stdout
//=======================================================================
  puts( text );
//=======================================================================
}

One of the things I am looking to do is figure out how to get the GUI
version to "intercept" this long output, writing the text into a read-only
QTextEdit widget instead of stdout.

--
 [ signature omitted ] 

Message 5 in thread

Essentially, QString is unicode, while stdout is 8bit
If I look at the macro qPrintable (in Qt 4.2.something):

#ifndef qPrintable
#  define qPrintable(string) (string).toLocal8Bit().constData()
#endif

QString::toLocal8Bit says:
QTextCodec::codecForLocale() is used to perform the conversion from Unicode.

Seems that by being gui app, locale have changed. Try printing out
used codec, for example by:
cout << QTextCodec::codecForLocale->name().constData() << endl;

for both gui and console. I guess the codec will be different for console/gui.

So in console the locale is bad? use

QTextCodec::setCodecForLocale(QTextCodec::codecForName("the-name-of-the-coded-that-was-used-in-gui-app"))

to set the good codec, the same as used by gui.

I think this should help ... if it won't, there are probably some
other differences between gui/console (used LANG/LC_ALL/etc ...)

Martin Petricek

On 4/15/07, Larry Bristol <larry@xxxxxxxxxxxxxx> wrote:
> I have observed a rather startling bit of behavior that I hope someone will
> be able to explain for me.
>
> I have two programs that perform a similar function.  In fact, they are
> essentially identical except that one of them is a "console" program (using
> stdin/stdout for all communications with the user) and the other is a
> QApplication (using GUI windows, dialogs and the like).
>
> Some of the common routines used by these programs write text to stdout
> regardless of this.  They no nothing at all about where they are running.
> There's no fooling around.  They use the standard "puts" function to write
> to stdout like this:
>         QString text;
>         puts( qPrintable(text) );
>
> Now it so happens that "text" occasionally contains unicode characters.  And
> therein lies the catch:  When I run the console version of the program, the
> unicode characters disappear from the output.  That was not a surprise to
> me, since there is no ASCII equivalent.
>
> But when I run the GUI version of the program, these same unicode characters
> are now displayed in the output!  I'm not talking about unicode characters
> appearing in some QWidget; I *expect* that to work.  I'm talking about
> unicode character representations appearing in the stdout file!
>
> HUH?!?
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>

--
 [ signature omitted ] 

Message 6 in thread

BH wrote:

> Essentially, QString is unicode, while stdout is 8bit
> If I look at the macro qPrintable (in Qt 4.2.something):
> 
> #ifndef qPrintable
> #  define qPrintable(string) (string).toLocal8Bit().constData()
> #endif
> 
> QString::toLocal8Bit says:
> QTextCodec::codecForLocale() is used to perform the conversion from
> Unicode.
> 
> Seems that by being gui app, locale have changed. Try printing out
> used codec, for example by:
> cout << QTextCodec::codecForLocale->name().constData() << endl;
> 
> for both gui and console. I guess the codec will be different for
> console/gui.

That is an excellent suggestion!

I put the following into the initialization logic for both:
  MyClass::log(QTextCodec::codecForLocale()->name().constData());
I expected them to be different, but to my surprise, both programs reported
the name "System".

I guess this is *still* curious behavior!  <grin>

--
 [ signature omitted ]