[PATCH] Ask Qt 4 to use the UTF-8 codec as the "codec for C strings"

Thiago Macieira thiago at macieira.org
Thu Apr 25 15:28:31 PDT 2013

Qt 5 does this by default, so it's not necessary there (in fact,
setCodecForCStrings was removed, so you catch any mistakes).

Now all QString methods taking a const char* or QByteArray
(constructor, append(), operator+=, operator<, etc.) will interpret
that char array as UTF-8. Conversely, the QByteArray methods taking a
QString will generate UTF-8 too. This includes the badly named
QString::fromAscii() and QString::toAscii().

Signed-off-by: Thiago Macieira <thiago at macieira.org>
 qt-gui.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/qt-gui.cpp b/qt-gui.cpp
index d68071d..d13c3c4 100644
--- a/qt-gui.cpp
+++ b/qt-gui.cpp
@@ -33,6 +33,7 @@
 #include <QFileDialog>
 #include <QFileInfo>
 #include <QStringList>
+#include <QTextCodec>
 #include <QTranslator>
@@ -1874,6 +1875,15 @@ void init_qt_ui(int *argcp, char ***argvp)
 void init_ui(int *argcp, char ***argvp)
 	application = new QApplication(*argcp, *argvp);
+#if QT_VERSION < 0x050000
+	// ask QString in Qt 4 to interpret all char* as UTF-8,
+	// like Qt 5 does.
+	// 106 is "UTF-8", this is faster than lookup by name
+	// [http://www.iana.org/assignments/character-sets/character-sets.xml]
+	QTextCodec::setCodecForCStrings(QTextCodec::codecForMib(106));
 	GtkWidget *win;
 	GtkWidget *nb_page;
 	GtkWidget *dive_list;

More information about the subsurface mailing list