0

I'm trying to do something very simple, I just want to print my native language, pt-br in Windows Console.

IDE Creator I created a new project->other->Qt Console Application the I put it in my main.cpp file:

#include <QCoreApplication>
#include <QDebug>
#include <QTextCodec>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication  a(argc, argv);

    qDebug() << "aeiou áéíóú";
    std::cout << "aeiou áéíóú" << endl;

    return 0;
}

here is what I got:

C:\Users\maiko.costa\testeQtConsole\debug>testeQtConsole.exe
aeiou ßÚݾ·
aeiou ßÚݾ·

C:\Users\maiko.costa\testeQtConsole\debug>

I've tried it too, but with the same previous output:

#include <QCoreApplication>
#include <QDebug>
#include <QTextCodec>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    QCoreApplication  a(argc, argv);

    QTextCodec *codec = QTextCodec::codecForName("CP1252");
    QTextCodec::setCodecForCStrings(codec);

    qDebug() << "aeiou áéíóú";
    std::cout << "aeiou áéíóú" << endl;

    return 0;
}

The System encode for Windows 7 is it right ?

what am I missing ?

MaikoID
  • 4,359
  • 4
  • 25
  • 27
  • Perhaps check out [QLocale](http://www.trinitydesktop.org/docs/qt4/qlocale.html)? – AJG85 Feb 10 '12 at 15:58
  • Have you tried escaping the non-ASCII characters in the string literals? – pezcode Feb 10 '12 at 18:15
  • QLocale didn't change anything. @pezcode did you mean something like this: http://stackoverflow.com/questions/3211214/how-to-convert-a-unicode-string-to-its-unicode-escapes ? – MaikoID Feb 13 '12 at 12:24
  • @MaikoID yes. Not sure which compiler you're using, but unescaped non-ASCII literals are not guaranteed by the C++ standard. IIRC VS only allows those inside `wchar_t` literals (`L""`) – pezcode Feb 13 '12 at 13:33

4 Answers4

1

I am not that familiar with QT but I think this can help you just as well. The Windows console uses the OEM char set. Therefore, in order to properly print characters on std::cout they need to be encoded using OEM. This can be accomplished using the Windows API CharToOem.

Small example, just so you get the idea (here input is assumed to be UTF16):

void oemPrint(const wchar_t* str) {
    char* chars = (char*)alloca(strlen(str)+1);
    CharToOemW(str, chars);
    fputs(chars, stdout);
}

// Usage:
oemPrint(L"aeiou áéíóú");

EDIT: A QT solution might be to use QTextCodec::codecForName("IBM 850") - this is the OEM codec.

rasmus
  • 3,136
  • 17
  • 22
  • You also have to check the source code encoding and the compiler's documentation to make sure that the string litteral is correctly interpreted before it is encoded for output. – André Caron Feb 10 '12 at 22:22
  • Yes, that is why I wrote "here input is assumed to be UTF16". It is just an example. – rasmus Feb 10 '12 at 22:28
  • The Windows console is Unicode and the console subsystem will do any codepage conversions for you. Besides, the codepage *only* applies if you select a raster font for the console. – Joey Feb 10 '12 at 22:28
  • @Joey No, it is OEM. Try printing UTF16 or UTF8 directly to std::cout. It will not work. – rasmus Feb 10 '12 at 22:32
  • CP 850 isn't the universal OEM codepage, though. In Europe 437 is normal and other parts of the world have different ones as well. And the whole OEM/Unicode thing smells more like a C++ iostream problem than a limitation of the Windows console – WriteConsoleW *does* write Unicode, even to a console that has a different charset (if using Raster fonts conversion is performed by Windows). – Joey Feb 11 '12 at 09:34
  • Granted OEM, can be other code pages. WriteConsole does perform this conversion and might be simpler if std::cout isn't a requirement. The [doc page](http://msdn.microsoft.com/en-us/library/windows/desktop/ms687401(v=vs.85).aspx) for that function explains that the default char set indeed is OEM apparently you can change this using SetConsoleCP. Using this function might be the best solution. – rasmus Feb 11 '12 at 10:21
  • Hi, I've tried everything in this page without success. I've found a interesting post in qtforum and the guy couldn't do too. I'm waiting any answer here or there. http://www.qtcentre.org/threads/2344-Unicode-on-(Win32)-console?p=213823#post213823 – MaikoID Feb 13 '12 at 13:20
  • It didn't compile. I can't find which headers are you using. – MaikoID Feb 13 '12 at 13:44
  • Includes: malloc.h, windows.h, stdio.h, string.h – rasmus Feb 13 '12 at 13:54
1

I find the solution in this thread. Output unicode strings in Windows console app

If I ran chcp 65001 in windows console before I ran my app the characters are printed correctly.

I don't know how to workaround it in my source code, then I call this program manually with the start cpp function.

Community
  • 1
  • 1
MaikoID
  • 4,359
  • 4
  • 25
  • 27
0

Here is the return line of function I wrote that displays passwords as ● ● ● ● ●

return QString::fromUtf8( "\u25CF \u25CF \u25CF \u25CF \u25CF" );

QString::fromUnicode should work the same.

Maybe something like:

QString x = QString::fromUtf8( "\u25CF \u25CF \u25CF \u25CF \u25CF" );
std::cout << qPrintable(x) << std::endl;

Of course change it to QString::fromUnicode... hope this helps

Wes
  • 4,781
  • 7
  • 44
  • 53
-1
QString a="aeiou áéíóú";
std::cout<< a.toStdString().data();
Lucifer
  • 29,392
  • 25
  • 90
  • 143