Skip to content

Commit 906ac59

Browse files
jbowlerjcfr
authored andcommitted
[Backport generator] Qt6: QTextStream compatibility (#120)
- provides compatibility with the removal of the global ::endl for use with QTextStream; Qt6 requires Qt::endl - includes Qt6 specific changes necessary to deal with the replacement of QTextCodec by QStringConverter and the slight changes to the default setup of QTextStream (to UTF-8 with auto-detect of UTF-16) --------- Signed-off-by: John Bowler <[email protected]> (cherry picked from commit MeVisLab/pythonqt@d11fc4f)
1 parent 2d6058e commit 906ac59

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

generator/abstractmetabuilder.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
#include <QtCore/QDebug>
5757
#include <QtCore/QFile>
5858
#include <QtCore/QFileInfo>
59-
#include <QtCore/QTextCodec>
59+
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
60+
# include <QtCore/QTextCodec>
61+
#endif
6062
#include <QtCore/QTextStream>
6163
#include <QtCore/QVariant>
6264

@@ -402,7 +404,10 @@ bool AbstractMetaBuilder::build()
402404
return false;
403405

404406
QTextStream stream(&file);
405-
stream.setCodec(QTextCodec::codecForName("UTF-8"));
407+
# if QT_VERSION < QT_VERSION_CHECK(6,0,0)
408+
stream.setCodec(QTextCodec::codecForName("UTF-8"));
409+
/* Note required in Qt6: see the same call in asttoxml.cpp */
410+
# endif
406411
QByteArray contents = stream.readAll().toUtf8();
407412
file.close();
408413

generator/asttoxml.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@
4747

4848
#include <QXmlStreamWriter>
4949
#include <QTextStream>
50-
#include <QTextCodec>
50+
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
51+
# include <QTextCodec>
52+
#endif
5153
#include <QFile>
5254

5355
void astToXML(QString name) {
@@ -57,7 +59,18 @@ void astToXML(QString name) {
5759
return;
5860

5961
QTextStream stream(&file);
62+
#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
6063
stream.setCodec(QTextCodec::codecForName("UTF-8"));
64+
#else
65+
/* NOTE, for Qt6:
66+
*
67+
* stream.setEncoding(QStringConverter::Utf8)
68+
*
69+
* is the default but will be overridden if the UTF-16 BOM is seen. This
70+
* is almost certainly the correct behavior because the BOM isn't valid in
71+
* a text stream otherwise.
72+
*/
73+
#endif
6174
QByteArray contents = stream.readAll().toUtf8();
6275
file.close();
6376

@@ -164,7 +177,7 @@ void writeOutClass(QXmlStreamWriter &s, ClassModelItem &item) {
164177
writeOutEnum(s, enumItem);
165178
}
166179

167-
QHash<QString, FunctionModelItem> functionMap = item->functionMap();
180+
QMultiHash<QString, FunctionModelItem> functionMap = item->functionMap();
168181
for (FunctionModelItem funcItem : functionMap.values()) {
169182
writeOutFunction(s, funcItem);
170183
}

generator/typesystem.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
#include <QtCore/QMap>
4949
#include <QDebug>
5050

51+
/* BEGIN: Qt6 compatibility. The following can removed when versions of Qt
52+
* prior to 5.14 are no longer supported.
53+
*/
5154
/* QString::SkipEmptyParts was replicated in Qt::SplitBehavior in 15.4 and the
5255
* QString original deprecated then it was removed in Qt6. This provides
5356
* forward compatibility with Qt6 for versions of Qt prior to 15.4:
@@ -58,6 +61,27 @@
5861
};
5962
#endif
6063

64+
/* Global endl (::endl) is used extensively in the generator .cpp files. This
65+
* was supported by Qt until Qt6. In Qt5.14 Qt::endl was added in anticipation
66+
* of the Qt6 change and the use of global endl could be avoided (it does not
67+
* seem to have been explicitly deprecated). This gives backward compatibility
68+
* for global endl in Qt6 (not Qt5, where global endl was still available).
69+
*
70+
* Note that 'constexpr' is available in Qt6 because Qt6 requires C++17;
71+
* constexpr was introduced in C++11. Likewise for decltype. Qt::endl is a
72+
* function so ::endl is a pointer to the function and the implicit conversion
73+
* is used; this is to cause an compiler error in the future if the base type
74+
* of Qt::endl changes.
75+
*
76+
* When versions of Qt older than 5.14 are no longer supported this can be
77+
* removed however all the 'endl' references in the code will need to be
78+
* changed to Qt::endl.
79+
*/
80+
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
81+
static const constexpr decltype (Qt::endl) *endl = Qt::endl;
82+
#endif
83+
/* END: Qt compatibility. */
84+
6185
class Indentor;
6286

6387
class AbstractMetaType;

0 commit comments

Comments
 (0)