Wrong output of qDebug() (UTF - 8) - c++

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Ó";

Related

QDomElement::namespaceURI returns empty string

I'm trying to get to the root of a problem in some library, and it seems like I don't understand how QDomElement::namespaceURI work, or don't understand how xmlns works.
Here is my minimal isolated code fragment:
#include <iostream>
#include <QString>
#include <QDomDocument>
int main() {
QString request = "<iq from='user2#example.org' id='disco1' to='user1#example.org' type='get'>";
request += "<query node='someAddress' xmlns='http://jabber.org/protocol/disco#info'/>";
request += "</iq>";
QDomDocument doc;
doc.setContent(request);
QDomElement iq = doc.documentElement();
QDomElement query = iq.firstChildElement();
std::cout << query.tagName().toStdString() << " " << query.namespaceURI().toStdString() << std::endl;
return 0;
}
I compile it this way
g++ -o xmlTest xmlTest.cpp -lQt5Core -lQt5Xml -I /usr/include/qt -I /usr/include/qt/QtCore -I /usr/include/qt/QtXml
I expect it to print query http://jabber.org/protocol/disco#info
Instead it prints query and I do not understand why.
What am I doing wrong?
By default QtXml does not do namespace processing. There is an optional bool namespaceProcessing flag you can pass to setContent() to enable it.
Modifying that line like so, produces the expected behaviour:
doc.setContent(request, true);
$ ./xmlTest
query http://jabber.org/protocol/disco#info

QLocale and QSettings

Premise: I'm on osx using qt5.7 I've changed the decimal separator in the System Preferences - Language and Region - Advanced to use the comma:
I have a problem in storing/restoring the QLocale value via QSettings.
This is the main.cpp:
#include <QSettings>
#include <QDebug>
void printLocale(QString header, QLocale locale) {
qDebug() <<
QLocale::languageToString(locale.language()) <<
QLocale::scriptToString(locale.script()) <<
QLocale::countryToString(locale.country()) <<
locale.decimalPoint() << "-" << header;
}
int main( int argc, char **argv )
{
QLocale my_loc=QLocale::system();
printLocale("System OK", my_loc);
QSettings my_set("test","");
my_set.setValue("locale",my_loc);
QLocale my_set_loc=my_set.value("locale").toLocale();
printLocale("QSettings NOT OK",my_set_loc);
// hack from https://stackoverflow.com/a/11603299/2743307
QLocale hungary(QLocale::Hungarian);
my_set_loc.setNumberOptions(hungary.numberOptions());
printLocale("Hungarian STILL NOT OK",my_set_loc);
return 0;
}
and this is my .pro:
TEMPLATE = app
QT += core
TARGET = test
INCLUDEPATH += .
SOURCES += main.cpp
The output is:
"English" "Latin" "UnitedStates" ',' - "System OK"
"English" "Latin" "UnitedStates" '.' - "QSettings NOT OK"
"English" "Latin" "UnitedStates" '.' - "Hungarian STILL NOT OK"
and it looks like the QLocale is aware that I use comma as decimal separator but when this QLocale is stored in QSettings and read back, Qt does not recover it.
Also when trying the hack described here: https://stackoverflow.com/a/11603299/2743307 it doesn't work.
It seems to be a bug. I've just tested your code using 5.6 and macOS Sierra (10.12.3) and it works correctly, even without the hack.
When testing on Qt 5.8 it stopped working, but if you change the initialization of the QSettings to save settings to a file, it works!
// QSettings my_set("test","");
QSettings my_set("test.ini", QSettings::IniFormat);

Printing Qt variables

I'm programming in Qt, but I'm more accustomed to PHP.
So with that in mind, how do I 'echo' or 'print' out the contents of a QStringList or QString to ensure the contents are as expected?
I'm building a GUI application. Is there anyway to print the contents?
Obviously in PHP, you can print_r on an array, is there anything similar for a QStringList?
And echo a variable, again, anything similar to QString?
I can provide code if needs be.
Thanks.
main.cpp
#include <QStringList>
#include <QDebug>
int main()
{
QStringList myStringList{"Foo", "Bar", "Baz"};
qDebug() << myStringList;
QString myString = "Hello World!";
qDebug() << myString;
return 0;
}
main.pro
TEMPLATE = app
TARGET = print-qstringlist
QT = core
CONFIG += c++11
SOURCES += main.cpp
Build and Run
qmake && (n)make
Output
("Foo", "Bar", "Baz")
"Hello World!"
If you need to drop the noisy brackets and double quotes generated by qDebug, you are free to either use QTextStream with custom printing or simply fall back to the standard cout with custom printing.

Qt Lang environment?

I Have got an issue concerning Qt Locale environment when I execute the following code
QApplication(argc,argv) ;
float f = 42.5f
std::cout << std::to_string(f) ; // prints 42,5
Even if my computer got its locale to french I'd like my program to be compiled with us standard printing format (i.e. 42.5 ). Is there a way to do that with a compiler option ?
This works fine for me:
main.cpp
#include <QString>
#include <QDebug>
#include <QCoreApplication>
#include <QLocale>
int main(int argc, char **argv)
{
QCoreApplication coreApplication(argc, argv);
float f = 42.5f;
qDebug() << QString::number(f, 'f', 1);
QLocale locale;
qDebug() << locale.toString(f, 'f', 1);
return coreApplication.exec();
}
main.pro
TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp
Build and Run
qmake && make && ./main
Output
"42.5"
"42.5"
This may be interesting for you:
QString QString::number(double n, char format = 'g', int precision = 6) [static]
Returns a string equivalent of the number n, formatted according to the specified format and precision. See Argument Formats for details.
Unlike QLocale::toString(), this function does not honor the user's locale settings.

How to handle Korean character set between QT & Oracle

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).