QLocale and QSettings - c++

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

Related

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.

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.

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

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

cout does no print in QtCreator

I saw this question already on this forum but I do not know why the proposed answer does not work in my case. So I try to ask for other slution.
I just got my Qt creator running under Linux.
I do not understand why my:
cout << "This does not appear";
Does not print in console while qdebug does
qDebug() << "This appears";
This is what is contained in my .pro file:
QT += core gui
TARGET = aaa
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
IeplcModule.cpp
HEADERS += mainwindow.h \
IeplcModule.h
FORMS += mainwindow.ui
#enable console
CONFIG += console
Any idea?
Try with:
cout << "asdf" << endl;
Possibly Qt sets up iostream in order to flush only at new line.
When debugging with CDB (Windows debugger) and running application not in the dedicated terminal window, but within QtCreator output panel, there is an issue with std::cout/std::cerr.
qDebug works because it has a trick for this case.
So, the only solution in this case is enable the "run in terminal" option.
For more infor please follow the link above to the Qt bug tracker.
Is it possible that STDOUT is redirecting? qDebug prints to STDERR by default.
Did you #include <iostream>? I did not see any includes in the code.
I assume that qdebug and cout are very similar.
Make sure you have console config enabled in your .pro file. I.e. :
CONFIG += console
You can run this program from CMD and it will print some messages to the console:
/* Create a .pro file with this content:
QT += core gui widgets
SOURCES += main.cpp
TARGET = app
-------------------------------
Build and run commands for CMD:
> qmake -makefile
> mingw32-make
> "release/app"
*/
#ifdef _WIN32
#include <windows.h>
#endif
#include <QtCore/QFile>
#include <QtCore/QString>
#include <QtCore/QIODevice>
#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>
#include <iostream>
class Widget : public QWidget
{
public:
Widget()
{
setWindowTitle("My Title");
QString path("assets/text.txt");
std::cout << std::endl;
std::cout << "hello1" << std::endl;
std::cout << path.toStdString() << std::endl;
std::cout << "hello2" << std::endl;
}
};
int main(int argc, char *argv[])
{
#ifdef _WIN32
if (AttachConsole(ATTACH_PARENT_PROCESS))
{
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}
#endif
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}