How to handle Korean character set between QT & Oracle - c++

I'd like to use Oracle with ODBC.
I could get data from Oracle successfully. But Korean character is broken like ????.
As all programmer said on internet forum, I tried to apply QTextCodec like below.
I tried EUC-KR and other codec names. But no change.
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QSqlQuery q("select * from temp", db);
q.setForwardOnly(true);
QString contect= "";
while(q.next())
{
QByteArray name = q.value(0).toByteArray();
QString age = q.value(1).toString();
contect = contect + codec->toUnicode(name);
ui.textEdit->setText(contect);
}
Oracle side info is.....
NLS_CHARACTERSET : KO16MSWIN949
NLS_NCHAR_CHARACTERSET : AL16UTF16
NLS_LANG : KOREAN_KOREA.KO16MSWIN949
I'm developing with eclipse (on windows 7) and the default file text encoding is utf-8.
I'll appreciate it if you give me comment.
Thanks.

I think you need to change the codec name as you need a codec from the Korean character set into UTF8.
Try changing your code to:
QTextCodec *codec = QTextCodec::codecForName("cp949");
As the Wikipedia page for Code page 949 mentions that it is non-standard Microsoft version of EUC-KR, you could also try EUC-KR.
Try the following program to get the list of text codecs and aliases:
test.cpp
#include <QtCore>
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
const auto codecs = QTextCodec::availableCodecs();
for (auto it = codecs.begin(); it != codecs.end(); ++it)
{
const auto codec = QTextCodec::codecForName(*it);
qDebug() << codec->name() << codec->aliases();
}
return 0;
}
test.pro
QT += core
SOURCES=test.cpp
QMAKE_CXXFLAGS += -std=c++0x
Note that the program uses auto for brevity, but this requires a C++11 compiler (tested on GCC 4.4).

Related

Qt QProcess sometimes works and sometimes does not work

I want to convert a ps file to pdf by ps2pdf and this codes in Qt:
QPixmap graphPng(filePath + "report/pictures/graph-000.png");
int graphPsWidth=graphPng.width();
int graphPsHeight=graphPng.height();
//convert "ps" file to "pdf" file
QProcess process;
QStringList arg;
arg<<"-dDEVICEWIDTHPOINTS=" + QString::number(graphPsWidth)<<"-dDEVICEHEIGHTPOINTS=" + QString::number(graphPsHeight)<<"export/report/pictures/graph-000.ps"<<"export/report/pictures/graph-000.pdf";
process.start("ps2pdf",arg);
//process.waitForStarted(-1);
process.waitForFinished(-1);
(I use a png file as the same dimensions of the ps file to get the dimensions and use it in creating pdf file)
I don't know why sometimes the pdf file is created and some times without any output message (process::readAllStandardError() and process::readAllStandardOutput()) there is no pdf file!
When the pdf file is not created if I run that immediately in terminal, the pdf file will be created!!!
What is the reason and how can I solve it?
It is always advisable to verify all the steps, so I share a more robust version of your code.
For example it is not necessary to use QPixmap, the correct thing is to use QImage. In addition, the path of the files is verified.
#include <QCoreApplication>
#include <QDir>
#include <QProcess>
#include <QImage>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir directory(QString("%1%2%3")
.arg(a.applicationDirPath())
.arg(QDir::separator())
.arg("export/report/pictures/"));
QString name = "graph-000";
QString png_path = directory.filePath(name + ".png");
QString ps_path = directory.filePath(name + ".ps");
// verify path
Q_ASSERT(QFileInfo(png_path).exists());
Q_ASSERT(QFileInfo(ps_path).exists());
QString pdf_path = directory.filePath(name+".pdf");
QImage img(png_path);
int graphPsWidth = img.width();
int graphPsHeight = img.height();
QProcess process;
QStringList arg;
arg << QString("-dDEVICEWIDTHPOINTS=%1").arg(graphPsWidth) <<
QString("-dDEVICEHEIGHTPOINTS=%1").arg(graphPsHeight) <<
ps_path << pdf_path;
process.start("ps2pdf",arg);
process.waitForFinished();
Q_ASSERT(QFileInfo(pdf_path).exists());
return 0;
}
I can solve the problem by handling the RAM, because if the ram exceed the specific limit (almost near the full capacity of the RAM), the QProcess can not be started. So every time it diagnoses the exceeded limit, the PDF file is not created and vice versa.

Qt convert unicode entities

In QT 5.4 and C++ I try to decode a string that has unicode entities.
I have this QString:
QString string = "file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt";
I want to convert this string to this: fileÖÇŞİĞÜİçöşiğüı.txt
I tried QString's toUtf8 and fromUtf8 methods. Also tried to decode it character by character.
Is there a way to convert it by using Qt?
Qt provides a macro called QStringLiteral for handling string literals correctly.
Here's a full working example:
#include <QString>
#include <QDebug>
int main(void) {
QString string = QStringLiteral("file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt");
qDebug() << string;
return 0;
}
As mentioned in the above comments, you do need to print to a console that supports these characters for this to work.
I have just tested this code:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QString s = "file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt";
qDebug() << s.length(); //Outputs: 22
qDebug() << s; //Outputs: fileÖÇŞİĞÜİçöşiğüı.txt
return a.exec();
}
This is with Qt 5.4 on ubuntu, so it looks like your problem is with some OS only.
#include <QTextDocument>
QTextDocument doc;
QString string = "file\u00d6\u00c7\u015e\u0130\u011e\u00dc\u0130\u00e7\u00f6\u015fi\u011f\u00fc\u0131.txt";
doc.setHtml(string); // to convert entities to text
QString result = doc.toPlainText(); // result = "fileÖÇŞİĞÜİçöşiğüı.txt"
NOT USEFUL if you have a CONSOLE app
QTextDocument needs the GUI module.

How to force QLocale::system to change

I need to test my application translation to non-English language very often, and this is very uncomfortable to change the whole operating system language just to do this simple check.
How can i change Qt-detected system language using environment variables for example?
or with command-line parameter.
I try to change LANG, LANGUAGE environment variables, but it has no effect.
However, under GNOME it has!
UPD: code i'm using such code to determine the system locale and load appropriate translation:
QTranslator app_translator;
if (!app_translator.load ("app_" + QLocale::system ().name (), app_tr_dir))
qWarning ("Can't load app translator file for locale %s from %s", qPrintable (QLocale::system ().name ()), app_tr_dir.toLocal8Bit().data());
else
app.installTranslator (&app_translator);
P.S. My OS is Kubuntu 13.10, Qt version is 4.8.
You can always change the locale by QLocale::setDefault() method. here's an example from one project:
void Language::setCurrentLanguage(Language::Languages language)
{
if (language == Language::Arabic) {
QLocale l(QLocale::Arabic, QLocale::SaudiArabia);
QLocale::setDefault(l);
dynamic_cast<MangoApp*>(qApp)->setLayoutDirection(Qt::RightToLeft);
dynamic_cast<MangoApp*>(qApp)->removeAllTranslator();
dynamic_cast<MangoApp*>(qApp)->loadQtTranslator();
dynamic_cast<MangoApp*>(qApp)->loadMangoTranslator();
} else {
QLocale l(QLocale::English, QLocale::UnitedStates);
QLocale::setDefault(l);
dynamic_cast<MangoApp*>(qApp)->setLayoutDirection(Qt::LeftToRight);
dynamic_cast<MangoApp*>(qApp)->removeAllTranslator();
}
}
For testing you can use something like that (just correct main function):
int main(int argc, char **argv) {
QApplication app(argc, argv);
QLocale localeUsedToDeterminateTranslators = QLocale::system();
Q_FOREACH(QString a, app.arguments()) {
const static localeParam = "-locale:";
if (a.startsWith(localeParam)) {
localeUsedToDeterminateTranslators = QLocale(a.mid(sizeof(localeParam)-1));
break;
}
}
... // your normal code
Then when you run you app you can just run it with extra parameter: ./yourAppName -locale:nl. See documentation of QLocale for possible values.
Edit: I've found even better approach, there is a method QLocale::setDefault, so this should work even better:
int main(int argc, char **argv) {
QApplication app(argc, argv);
Q_FOREACH(QString a, app.arguments()) {
const static localeParam = "-locale:";
if (a.startsWith(localeParam)) {
QLocale::setDefault(QLocale(a.mid(sizeof(localeParam)-1)));
break;
}
}
...
QTranslator app_translator;
if (!app_translator.load ("app_" + QLocale().name (), app_tr_dir))
qWarning ("Can't load app translator file for locale %s from %s", qPrintable (QLocale().name()), app_tr_dir.toLocal8Bit().data());
else
app.installTranslator (&app_translator);
Using the LANGUAGE (not LANG) environment variable should definitely change the value returned by QLocale::system().name(), because this environment variable has precedence over all other ways to define the application message locale (details).
I tested it this with Qt 5.12 under Lubuntu 19.10 (means, using the LXQt desktop), and it works. The command was:
LANGUAGE=de ./application
If this really does not work under Kubuntu, it should be reported as a bug, because then Kubuntu is interfering with the way how an application is told its locale.

Wrong output of qDebug() (UTF - 8)

I'm trying to store a string with special chars::
qDebug() << "ÑABCgÓ";
Outputs: (here i can't even type the correct output some garbage is missing after à & Ã)
ÃABCgÃ
I suspect some UTF-8 / Latin1 / ASCII, but can't find the setting to output to console / file. What i have written in my code : "ÑABCgÓ".
(Qt:4.8.5 / Ubunto 12.04 / C++98)
You could use the QString QString::fromUtf8(const char * str, int size = -1) [static] as the sample code presents that below. This is one of the main reasons why QString exists.
See the documentation for details:
http://qt-project.org/doc/qt-5.1/qtcore/qstring.html#fromUtf8
main.cpp
#include <QString>
#include <QDebug>
int main()
{
qDebug() << QString::fromUtf8("ÑABCgÓ");
return 0;
}
Building (customize for your scenario)
g++ -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core main1000.cpp && ./a.out
Output
"ÑABCgÓ"
That being said, depending on your locale, simply qDebug() << "ÑABCgÓ"; could work as well like in here, but it is recommended to make sure by explicitly asking the UTF-8 handling.
Try this:
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForCStrings(codec);
qDebug() << "ÑABCgÓ";

QPrinter runtime error

EDIT: I managed to get a compiling and non crashing version. The only thing left is to get the desired output, however this particular question (why it crashes) has been answered so I am closing the question. I will post the working code before the broken one.
Good day! I am trying to create a small example that will simply create a pdf document. Everything compiles, however when the program starts it simply crashes. I am using Qt version 5.0.0
---New working code---
int main( int argc, char **argv )
{
QApplication app( argc, argv );
QTextDocument doc;
doc.setHtml( "<p>A QTextDocument can be used to present formatted text "
"in a nice way.</p>"
"<p align=center>It can be <b>formatted</b> "
"<font size=+2>in</font> <i>different</i> ways.</p>"
"<p>The text can be really long and contain many "
"paragraphs. It is properly wrapped and such...</p>" );
QPrinter printer;
printer.setOutputFileName("C:\\Users\\SameTime\\Desktop\\Cutie\\PDFPrintMaybe");
printer.setOutputFormat(QPrinter::PdfFormat);
doc.print(&printer);
printer.newPage();
return 0;
}
Here is the project code:
#-------------------------------------------------
#
# Project created by QtCreator 2013-06-08T10:07:11
#
#-------------------------------------------------
QT += core
QT -= gui
QT += printsupport
TARGET = PDFPrintMaybe
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
----Old code with error---
And here is the main cpp:
#include <QCoreApplication>
#include <QTextDocument>
#include <QPrinter>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextDocument doc;
doc.setHtml("<h1>Testing, testing, is this thing on?!</h1>");
QPrinter printer;
printer.setOutputFileName("C:\\Users\\SameTime\\Desktop\\Cutie\\PDFPrintMaybe");
printer.setOutputFormat(QPrinter::PdfFormat);
doc.print(&printer);
printer.newPage();
return a.exec();
}
I am a bit at a loss since it is compiling but crashing (almost) instantly when ran.
Try to create the objects on the heap otherwise they get automatically deleted when they ran out of scope, then the framework probably tries to delete them again and crashes.
QTextDocument *doc = new QTextDocument();
doc->setHtml("<h1>Testing, testing, is this thing on?!</h1>");
QPrinter* printer = new QPrinter();
printer->setOutputFileName("C:\\Users\\SameTime\\Desktop\\Cutie\\PDFPrintMaybe");
printer->setOutputFormat(QPrinter::PdfFormat);
doc->print(printer);
printer->newPage();