Skip to content

Commit 10a3e49

Browse files
richard42mrbean-bremen
authored andcommitted
Qt6 compatibility fixes for core PythonQt library
1 parent bd2fc94 commit 10a3e49

13 files changed

+47
-50
lines changed

src/PythonQt.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
122122
} else {
123123
qRegisterMetaType<quint32>("size_t");
124124
}
125-
int stringRefId = qRegisterMetaType<QStringRef>("QStringRef");
126-
PythonQtConv::registerMetaTypeToPythonConverter(stringRefId, PythonQtConv::convertFromStringRef);
127125

128126
int objectPtrListId = qRegisterMetaType<QList<PythonQtObjectPtr> >("QList<PythonQtObjectPtr>");
129127
PythonQtConv::registerMetaTypeToPythonConverter(objectPtrListId, PythonQtConv::convertFromQListOfPythonQtObjectPtr);
@@ -239,7 +237,7 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
239237
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QLineF);
240238
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QPoint);
241239
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QPointF);
242-
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QRegExp);
240+
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QRegularExpression);
243241

244242
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QFont);
245243
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QPixmap);
@@ -257,7 +255,6 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
257255
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QPen);
258256
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QTextLength);
259257
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QTextFormat);
260-
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QMatrix);
261258

262259
PyObject* pack = PythonQt::priv()->packageByName("QtCore");
263260
PyObject* pack2 = PythonQt::priv()->packageByName("Qt");

src/PythonQtClassInfo.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,8 @@ QStringList PythonQtClassInfo::memberList()
554554
}
555555
}
556556

557-
return QSet<QString>::fromList(l).toList();
557+
QSet<QString> set(l.begin(), l.end());
558+
return set.values();
558559
}
559560

560561
const QByteArray& PythonQtClassInfo::className() const

src/PythonQtClassWrapper.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,8 @@ static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
463463
}
464464
PyObject* dict = PyDict_New();
465465

466-
QSet<QString> completeSet = QSet<QString>::fromList(wrapper->classInfo()->memberList());
467-
completeSet.unite(QSet<QString>::fromList(wrapper->classInfo()->propertyList()));
466+
QSet<QString> completeSet(wrapper->classInfo()->memberList().begin(), wrapper->classInfo()->memberList().end());
467+
completeSet.unite(QSet<QString>(wrapper->classInfo()->propertyList().begin(), wrapper->classInfo()->propertyList().end()));
468468

469469
Q_FOREACH (QString name, completeSet) {
470470
if (name.startsWith("py_")) {

src/PythonQtConversion.cpp

+18-23
Original file line numberDiff line numberDiff line change
@@ -1015,17 +1015,17 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
10151015
if (wrap->classInfo()->isCPPWrapper()) {
10161016
if (wrap->classInfo()->metaTypeId()>0) {
10171017
// construct a new variant from the C++ object if it has a meta type (this will COPY the object!)
1018-
v = QVariant(wrap->classInfo()->metaTypeId(), wrap->_wrappedPtr);
1018+
v = QVariant(QMetaType(wrap->classInfo()->metaTypeId()), wrap->_wrappedPtr);
10191019
} else {
10201020
// TODOXXX we could as well check if there is a registered meta type for "classname*", so that we may pass
10211021
// the pointer here...
10221022
// is this worth anything? we loose the knowledge of the cpp object type
1023-
v = qVariantFromValue(wrap->_wrappedPtr);
1023+
v = QVariant::fromValue(wrap->_wrappedPtr);
10241024
}
10251025
} else {
10261026
// this gives us a QObject pointer
10271027
QObject* myObject = wrap->_obj;
1028-
v = qVariantFromValue(myObject);
1028+
v = QVariant::fromValue(myObject);
10291029
}
10301030
return v;
10311031
} else if (val == Py_None) {
@@ -1073,55 +1073,55 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
10731073
case QMetaType::Float:
10741074
{
10751075
float d = (float) PyObjGetDouble(val,false,ok);
1076-
if (ok) v = qVariantFromValue(d);
1076+
if (ok) v = QVariant::fromValue(d);
10771077
}
10781078
break;
10791079
case QMetaType::Long:
10801080
{
10811081
long d = (long) PyObjGetLongLong(val,false,ok);
1082-
if (ok) v = qVariantFromValue(d);
1082+
if (ok) v = QVariant::fromValue(d);
10831083
}
10841084
break;
10851085
case QMetaType::ULong:
10861086
{
10871087
unsigned long d = (unsigned long) PyObjGetLongLong(val,false,ok);
1088-
if (ok) v = qVariantFromValue(d);
1088+
if (ok) v = QVariant::fromValue(d);
10891089
}
10901090
break;
10911091
case QMetaType::LongLong:
10921092
{
10931093
qint64 d = PyObjGetLongLong(val, false, ok);
1094-
if (ok) v = qVariantFromValue(d);
1094+
if (ok) v = QVariant::fromValue(d);
10951095
}
10961096
break;
10971097
case QMetaType::ULongLong:
10981098
{
10991099
quint64 d = PyObjGetULongLong(val, false, ok);
1100-
if (ok) v = qVariantFromValue(d);
1100+
if (ok) v = QVariant::fromValue(d);
11011101
}
11021102
break;
11031103
case QMetaType::Short:
11041104
{
11051105
short d = (short) PyObjGetInt(val,false,ok);
1106-
if (ok) v = qVariantFromValue(d);
1106+
if (ok) v = QVariant::fromValue(d);
11071107
}
11081108
break;
11091109
case QMetaType::UShort:
11101110
{
11111111
unsigned short d = (unsigned short) PyObjGetInt(val,false,ok);
1112-
if (ok) v = qVariantFromValue(d);
1112+
if (ok) v = QVariant::fromValue(d);
11131113
}
11141114
break;
11151115
case QMetaType::Char:
11161116
{
11171117
char d = (char) PyObjGetInt(val,false,ok);
1118-
if (ok) v = qVariantFromValue(d);
1118+
if (ok) v = QVariant::fromValue(d);
11191119
}
11201120
break;
11211121
case QMetaType::UChar:
11221122
{
11231123
unsigned char d = (unsigned char) PyObjGetInt(val,false,ok);
1124-
if (ok) v = qVariantFromValue(d);
1124+
if (ok) v = QVariant::fromValue(d);
11251125
}
11261126
break;
11271127

@@ -1189,7 +1189,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
11891189
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)val;
11901190
if (wrap->classInfo()->isCPPWrapper() && wrap->classInfo()->metaTypeId() == type) {
11911191
// construct a new variant from the C++ object if it has the same meta type
1192-
v = QVariant(type, wrap->_wrappedPtr);
1192+
v = QVariant(QMetaType(type), wrap->_wrappedPtr);
11931193
} else {
11941194
// Try to convert the object to a QVariant based on the typeName
11951195
bool ok;
@@ -1202,10 +1202,10 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
12021202
void* object = castWrapperTo(wrap, typeName, ok);
12031203
if (ok) {
12041204
if (isPtr) {
1205-
v = QVariant(type, &object);
1205+
v = QVariant(QMetaType(type), &object);
12061206
}
12071207
else {
1208-
v = QVariant(type, object);
1208+
v = QVariant(QMetaType(type), object);
12091209
}
12101210
}
12111211
}
@@ -1215,7 +1215,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
12151215
PythonQtConvertPythonToMetaTypeCB* converter = _pythonToMetaTypeConverters.value(type);
12161216
if (converter) {
12171217
// allocate a default object of the needed type:
1218-
v = QVariant(type, (const void*)nullptr);
1218+
v = QVariant(QMetaType(type), (const void*)nullptr);
12191219
// now call the converter, passing the internal object of the variant
12201220
ok = (*converter)(val, (void*)v.constData(), type, true);
12211221
if (!ok) {
@@ -1226,7 +1226,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
12261226
const PythonQtMethodInfo::ParameterInfo& info = PythonQtMethodInfo::getParameterInfoForMetaType(type);
12271227
if (info.isQList && (info.innerNamePointerCount == 1)) {
12281228
// allocate a default object of the needed type:
1229-
v = QVariant(type, (const void*)nullptr);
1229+
v = QVariant(QMetaType(type), (const void*)nullptr);
12301230
ok = ConvertPythonListToQListOfPointerType(val, (QList<void*>*)v.constData(), info, true);
12311231
if (!ok) {
12321232
v = QVariant();
@@ -1461,7 +1461,7 @@ QString PythonQtConv::CPPObjectToString(int type, const void* data) {
14611461
// this creates a copy, but that should not be expensive for typical simple variants
14621462
// (but we do not want to do this for our won user types!
14631463
if (type>0 && type < (int)QVariant::UserType) {
1464-
QVariant v(type, data);
1464+
QVariant v(QMetaType(type), data);
14651465
r = v.toString();
14661466
}
14671467
}
@@ -1483,11 +1483,6 @@ PyObject* PythonQtConv::createCopyFromMetaType( int type, const void* data )
14831483
return (PyObject*)wrap;
14841484
}
14851485

1486-
PyObject* PythonQtConv::convertFromStringRef(const void* inObject, int /*metaTypeId*/)
1487-
{
1488-
return PythonQtConv::QStringToPyObject(((QStringRef*)inObject)->toString());
1489-
}
1490-
14911486
QByteArray PythonQtConv::getCPPTypeName(PyObject* type)
14921487
{
14931488
QByteArray result;

src/PythonQtConversion.h

-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ class PYTHONQT_EXPORT PythonQtConv {
185185
static PyObject* convertFromPythonQtSafeObjectPtr(const void* /* PythonQtObjectPtr* */ inObject, int /*metaTypeId*/);
186186
static bool convertToQListOfPythonQtObjectPtr(PyObject* obj, void* /* QList<PythonQtObjectPtr>* */ outList, int /*metaTypeId*/, bool /*strict*/);
187187
static PyObject* convertFromQListOfPythonQtObjectPtr(const void* /* QList<PythonQtObjectPtr>* */ inObject, int /*metaTypeId*/);
188-
static PyObject* convertFromStringRef(const void* inObject, int /*metaTypeId*/);
189188

190189
//! Returns the name of the equivalent CPP type (for signals and slots)
191190
static QByteArray getCPPTypeName(PyObject* type);

src/PythonQtImporter.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args)
222222
{
223223
PythonQtImporter *self = (PythonQtImporter *)obj;
224224
PyObject *code = nullptr, *mod = nullptr, *dict = nullptr;
225-
char *fullname;
225+
char *fullname = NULL;
226226

227227
if (!PyArg_ParseTuple(args, "s:PythonQtImporter.load_module",
228228
&fullname))
@@ -338,7 +338,7 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args)
338338
QVariantList list = result.toList();
339339
if (list.count()==3) {
340340
// We prepend the full module name (including package prefix)
341-
list.prepend(fullname);
341+
list.prepend(QString(fullname));
342342
#ifdef __linux
343343
#ifdef _DEBUG
344344
// imp_find_module() does not respect the debug suffix '_d' on Linux,
@@ -740,7 +740,7 @@ PythonQtImport::getCodeFromData(const QString& path, int isbytecode,int /*ispack
740740
QDateTime time;
741741
time = PythonQt::importInterface()->lastModifiedDate(path);
742742
QString cacheFilename = getCacheFilename(path, /*isOptimizedFilename=*/false);
743-
writeCompiledModule((PyCodeObject*)code, cacheFilename, time.toTime_t(), /*sourceSize=*/qdata.length());
743+
writeCompiledModule((PyCodeObject*)code, cacheFilename, time.toSecsSinceEpoch(), /*sourceSize=*/qdata.length());
744744
}
745745
}
746746
return code;
@@ -754,7 +754,7 @@ PythonQtImport::getMTimeOfSource(const QString& path)
754754
if (PythonQt::importInterface()->exists(path2)) {
755755
QDateTime t = PythonQt::importInterface()->lastModifiedDate(path2);
756756
if (t.isValid()) {
757-
mtime = t.toTime_t();
757+
mtime = t.toSecsSinceEpoch();
758758
}
759759
}
760760

src/PythonQtMethodInfo.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ int PythonQtMethodInfo::nameToType(const char* name)
372372
_parameterTypeDict.insert("QLineF", QMetaType::QLineF);
373373
_parameterTypeDict.insert("QPoint", QMetaType::QPoint);
374374
_parameterTypeDict.insert("QPointF", QMetaType::QPointF);
375-
_parameterTypeDict.insert("QRegExp", QMetaType::QRegExp);
375+
_parameterTypeDict.insert("QRegularExpression", QMetaType::QRegularExpression);
376376
_parameterTypeDict.insert("QFont", QMetaType::QFont);
377377
_parameterTypeDict.insert("QPixmap", QMetaType::QPixmap);
378378
_parameterTypeDict.insert("QBrush", QMetaType::QBrush);
@@ -388,7 +388,6 @@ int PythonQtMethodInfo::nameToType(const char* name)
388388
_parameterTypeDict.insert("QPen", QMetaType::QPen);
389389
_parameterTypeDict.insert("QTextLength", QMetaType::QTextLength);
390390
_parameterTypeDict.insert("QTextFormat", QMetaType::QTextFormat);
391-
_parameterTypeDict.insert("QMatrix", QMetaType::QMatrix);
392391
_parameterTypeDict.insert("QVariant", PythonQtMethodInfo::Variant);
393392
// own special types... (none so far, could be e.g. ObjectList
394393
}

src/PythonQtSlot.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ meth_get__doc__(PythonQtSlotFunctionObject * m, void * /*closure*/)
540540
if (!names.at(i - 1).isEmpty()) {
541541
doc += names.at(i - 1);
542542
} else {
543-
doc += QString('a' + i - firstArgOffset).toLatin1();
543+
doc += QString(QChar((char) ('a' + i - firstArgOffset))).toLatin1();
544544
}
545545
}
546546
doc += ")";

src/PythonQtStdDecorators.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "PythonQtConversion.h"
4646

4747
#include <QCoreApplication>
48+
#include <QRegularExpression>
4849

4950
bool PythonQtStdDecorators::connect(QObject* sender, const QByteArray& signal, PyObject* callable)
5051
{
@@ -240,7 +241,7 @@ QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* t
240241
return list;
241242
}
242243

243-
QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QRegExp& regExp)
244+
QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QRegularExpression& regExp)
244245
{
245246
const QMetaObject* meta = nullptr;
246247
QByteArray typeName;
@@ -322,7 +323,7 @@ int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, c
322323
return 0;
323324
}
324325

325-
int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QRegExp& regExp, QList<QObject*>& list)
326+
int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QRegularExpression& regExp, QList<QObject*>& list)
326327
{
327328
const QObjectList& children = parent->children();
328329
int i;
@@ -334,7 +335,8 @@ int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, c
334335
return -1;
335336

336337
// Skip if the name doesn't match.
337-
if (regExp.indexIn(obj->objectName()) == -1)
338+
QRegularExpressionMatch match = regExp.match(obj->objectName());
339+
if (match.hasMatch() == false)
338340
continue;
339341

340342
if ((typeName && obj->inherits(typeName)) ||

src/PythonQtStdDecorators.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <QMetaMethod>
5959
#include <QMetaEnum>
6060
#include <QMetaProperty>
61+
#include <QRandomGenerator>
6162

6263
class PYTHONQT_EXPORT PythonQtStdDecorators : public QObject
6364
{
@@ -82,7 +83,7 @@ public Q_SLOTS:
8283
const QObjectList* children(QObject* o);
8384
QObject* findChild(QObject* parent, PyObject* type, const QString& name = QString());
8485
QList<QObject*> findChildren(QObject* parent, PyObject* type, const QString& name= QString());
85-
QList<QObject*> findChildren(QObject* parent, PyObject* type, const QRegExp& regExp);
86+
QList<QObject*> findChildren(QObject* parent, PyObject* type, const QRegularExpression& regExp);
8687

8788
bool setProperty(QObject* o, const char* name, const QVariant& value);
8889
QVariant property(QObject* o, const char* name);
@@ -103,8 +104,8 @@ public Q_SLOTS:
103104
int static_Qt_qRound(double a) { return qRound(a); }
104105
qint64 static_Qt_qRound64(double a) { return qRound64(a); }
105106
const char* static_Qt_qVersion() { return qVersion(); }
106-
int static_Qt_qrand() { return qrand(); }
107-
void static_Qt_qsrand(uint a) { qsrand(a); }
107+
int static_Qt_qrand() { return QRandomGenerator::global()->generate(); }
108+
void static_Qt_qsrand(uint a) { QRandomGenerator::global()->seed(a); }
108109

109110
QString tr(QObject* obj, const QString& text, const QString& ambig = QString(), int n = -1);
110111

@@ -116,7 +117,7 @@ public Q_SLOTS:
116117
private:
117118
QObject* findChild(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name);
118119
int findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QString& name, QList<QObject*>& list);
119-
int findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QRegExp& regExp, QList<QObject*>& list);
120+
int findChildren(QObject* parent, const char* typeName, const QMetaObject* meta, const QRegularExpression& regExp, QList<QObject*>& list);
120121
};
121122

122123
class PythonQtSingleShotTimer : public QTimer

src/PythonQtVariants.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#include <QLineF>
5555
#include <QPoint>
5656
#include <QPointF>
57-
#include <QRegExp>
57+
#include <QRegularExpression>
5858

5959
#include <QFont>
6060
#include <QBitmap>
@@ -72,7 +72,6 @@
7272
#include <QPen>
7373
#include <QTextLength>
7474
#include <QTextFormat>
75-
#include <QMatrix>
7675

7776
#endif
7877

src/gui/PythonQtScriptingConsole.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class PYTHONQT_EXPORT PythonQtScriptingConsole : public QTextEdit
5555
Q_OBJECT
5656

5757
public:
58-
PythonQtScriptingConsole(QWidget* parent, const PythonQtObjectPtr& context, Qt::WindowFlags i = 0);
58+
PythonQtScriptingConsole(QWidget* parent, const PythonQtObjectPtr& context, Qt::WindowFlags i = Qt::WindowFlags());
5959

6060
~PythonQtScriptingConsole() override;
6161

src/src.pro

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# $Source$
55
# --------------------------------------------------
66

7-
TARGET = PythonQt-Qt5-PythonXY
7+
TARGET = PythonQt-Qt6-PythonXY
88
TEMPLATE = lib
99

1010
DESTDIR = ../lib
@@ -25,12 +25,16 @@ isEmpty(PYTHONQT_STATIC) {
2525

2626
DEFINES += PYTHONQT_CATCH_ALL_EXCEPTIONS
2727

28-
contains(QT_MAJOR_VERSION, 5) {
28+
contains(QT_MAJOR_VERSION, 6) {
2929
QT += widgets core-private
3030
}
3131

3232
INCLUDEPATH += $$PWD
3333

34+
macx {
35+
QMAKE_APPLE_DEVICE_ARCHS = x86_64 arm64
36+
}
37+
3438
include ( ../build/common.prf )
3539
include ( ../build/python.prf )
3640
TARGET = $$replace(TARGET, PythonXY, Python$${PYTHON_VERSION})

0 commit comments

Comments
 (0)