Skip to content

Commit 920249e

Browse files
hjmjohnsonjcfr
andcommitted
chore: Remove deprecated Qt5 paradigms
This changes deprecated Qt6 paradigms to either backward-compatible ones or uses preprocessor conditionals for Qt5 and Qt6 differences. This ensures compatibility with both Qt versions. Co-authored-by: Jean-Christophe Fillion-Robin <[email protected]>
1 parent 675ea74 commit 920249e

File tree

6 files changed

+195
-40
lines changed

6 files changed

+195
-40
lines changed

src/PythonQtClassInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ void PythonQtClassInfo::setupCPPObject(const QByteArray& classname)
102102
{
103103
_isQObject = false;
104104
_wrappedClassName = classname;
105+
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
106+
_metaTypeId = QMetaType::fromName(classname).id();
107+
#else
105108
_metaTypeId = QMetaType::type(classname);
109+
#endif
106110
if (_metaTypeId == 0) {
107111
_metaTypeId = -1;
108112
}

src/PythonQtConversion.cpp

Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,11 @@ PyObject* PythonQtConv::convertQtValueToPythonInternal(int type, const void* dat
270270
default:
271271
// check if we have a QList of pointers, which we can circumvent with a QList<void*>
272272
if (info.isQList && (info.innerNamePointerCount == 1)) {
273+
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
274+
static int id = QMetaType::fromName("QList<void*>").id();
275+
#else
273276
static int id = QMetaType::type("QList<void*>");
277+
#endif
274278
PythonQtArgumentFrame_ADD_VARIANT_VALUE_BY_ID(frame, id, ptr);
275279
// return the constData pointer that will be filled with the result value later on
276280
ptr = (void*)((QVariant*)ptr)->constData();
@@ -311,10 +315,17 @@ void* PythonQtConv::handlePythonToQtAutoConversion(int typeId, PyObject* obj, vo
311315
{
312316
void* ptr = alreadyAllocatedCPPObject;
313317

318+
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
319+
static int penId = QMetaType::fromName("QPen").id();
320+
static int brushId = QMetaType::fromName("QBrush").id();
321+
static int cursorId = QMetaType::fromName("QCursor").id();
322+
static int colorId = QMetaType::fromName("QColor").id();
323+
#else
314324
static int penId = QMetaType::type("QPen");
315325
static int brushId = QMetaType::type("QBrush");
316326
static int cursorId = QMetaType::type("QCursor");
317327
static int colorId = QMetaType::type("QColor");
328+
#endif
318329
static PyObject* qtGlobalColorEnum = PythonQtClassInfo::findEnumWrapper("Qt::GlobalColor", nullptr);
319330
if (typeId == cursorId) {
320331
static PyObject* qtCursorShapeEnum = PythonQtClassInfo::findEnumWrapper("Qt::CursorShape", nullptr);
@@ -728,7 +739,11 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
728739
if (info.typeId == PythonQtMethodInfo::Unknown || info.typeId >= QMetaType::User) {
729740
// check for QList<AnyPtr*> case, where we will use a QList<void*> QVariant
730741
if (info.isQList && (info.innerNamePointerCount == 1)) {
742+
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
743+
static int id = QMetaType::fromName("QList<void*>").id();
744+
#else
731745
static int id = QMetaType::type("QList<void*>");
746+
#endif
732747
if (!alreadyAllocatedCPPObject) {
733748
PythonQtArgumentFrame_ADD_VARIANT_VALUE_BY_ID_IF_NEEDED(alreadyAllocatedCPPObject, frame, id, ptr);
734749
ptr = (void*)((QVariant*)ptr)->constData();
@@ -1094,35 +1109,39 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
10941109
) {
10951110
// no special type requested
10961111
if (val == nullptr) {
1097-
type = QVariant::Invalid;
1112+
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
1113+
type = QMetaType::UnknownType;
1114+
#else
1115+
type = 0; // Equivalent to QVariant::Invalid or unregistered type
1116+
#endif
10981117
} else if (PyBytes_Check(val)) {
10991118
#ifdef PY3K
11001119
// In Python 3, it is a ByteArray
1101-
type = QVariant::ByteArray;
1120+
type = QMetaType::QByteArray;
11021121
#else
11031122
// In Python 2, we need to use String, since it might be a string
1104-
type = QVariant::String;
1123+
type = QMetaType::QString;
11051124
#endif
11061125
} else if (PyUnicode_Check(val)) {
1107-
type = QVariant::String;
1126+
type = QMetaType::QString;
11081127
} else if (val == Py_False || val == Py_True) {
1109-
type = QVariant::Bool;
1128+
type = QMetaType::Bool;
11101129
#ifndef PY3K
11111130
} else if (PyObject_TypeCheck(val, &PyInt_Type)) {
1112-
type = QVariant::Int;
1131+
type = QMetaType::Int;
11131132
#endif
11141133
} else if (PyLong_Check(val)) {
11151134
// return int if the value fits into that range,
11161135
// otherwise it would not be possible to get an int from Python 3
11171136
qint64 d = PyLong_AsLongLong(val);
11181137
if (d > std::numeric_limits<int>::max() ||
11191138
d < std::numeric_limits<int>::min()) {
1120-
type = QVariant::LongLong;
1139+
type = QMetaType::LongLong;
11211140
} else {
1122-
type = QVariant::Int;
1141+
type = QMetaType::Int;
11231142
}
11241143
} else if (PyFloat_Check(val)) {
1125-
type = QVariant::Double;
1144+
type = QMetaType::Double;
11261145
} else if (PyObject_TypeCheck(val, &PythonQtInstanceWrapper_Type)) {
11271146
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)val;
11281147
// c++ wrapper, check if the class names of the c++ objects match
@@ -1144,11 +1163,15 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
11441163
return v;
11451164
} else if (val == Py_None) {
11461165
// none is invalid
1147-
type = QVariant::Invalid;
1166+
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
1167+
type = QMetaType::UnknownType;
1168+
#else
1169+
type = 0; // Equivalent to QVariant::Invalid or unregistered type
1170+
#endif
11481171
} else if (PyDict_Check(val)) {
1149-
type = QVariant::Map;
1172+
type = QMetaType::QVariantMap;
11501173
} else if (PyList_Check(val) || PyTuple_Check(val) || PySequence_Check(val)) {
1151-
type = QVariant::List;
1174+
type = QMetaType::QVariantList;
11521175
} else {
11531176
// transport the Python objects directly inside of QVariant:
11541177
v = PythonQtObjectPtr(val).toVariant();
@@ -1157,28 +1180,32 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
11571180
}
11581181
// special type request:
11591182
switch (type) {
1160-
case QVariant::Invalid:
1183+
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
1184+
case QMetaType::UnknownType:
1185+
#else
1186+
case 0: // Equivalent to QVariant::Invalid or unregistered type
1187+
#endif
11611188
return v;
11621189
break;
1163-
case QVariant::Int:
1190+
case QMetaType::Int:
11641191
{
11651192
int d = PyObjGetInt(val, false, ok);
11661193
if (ok) return QVariant(d);
11671194
}
11681195
break;
1169-
case QVariant::UInt:
1196+
case QMetaType::UInt:
11701197
{
11711198
int d = PyObjGetInt(val, false,ok);
11721199
if (ok) v = QVariant((unsigned int)d);
11731200
}
11741201
break;
1175-
case QVariant::Bool:
1202+
case QMetaType::Bool:
11761203
{
11771204
int d = PyObjGetBool(val,false,ok);
11781205
if (ok) v = QVariant((bool)(d!=0));
11791206
}
11801207
break;
1181-
case QVariant::Double:
1208+
case QMetaType::Double:
11821209
{
11831210
double d = PyObjGetDouble(val,false,ok);
11841211
if (ok) v = QVariant(d);
@@ -1239,7 +1266,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
12391266
}
12401267
break;
12411268

1242-
case QVariant::ByteArray:
1269+
case QMetaType::QByteArray:
12431270
{
12441271
bool ok;
12451272
#ifdef PY3K
@@ -1249,20 +1276,20 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
12491276
#endif
12501277
}
12511278
break;
1252-
case QVariant::String:
1279+
case QMetaType::QString:
12531280
{
12541281
bool ok;
12551282
v = QVariant(PyObjGetString(val, false, ok));
12561283
}
12571284
break;
12581285

1259-
case QVariant::Map:
1286+
case QMetaType::QVariantMap:
12601287
pythonToMapVariant<QVariantMap>(val, v);
12611288
break;
1262-
case QVariant::Hash:
1289+
case QMetaType::QVariantHash:
12631290
pythonToMapVariant<QVariantHash>(val, v);
12641291
break;
1265-
case QVariant::List:
1292+
case QMetaType::QVariantList:
12661293
{
12671294
bool isListOrTuple = PyList_Check(val) || PyTuple_Check(val);
12681295
if (isListOrTuple || PySequence_Check(val)) {
@@ -1288,7 +1315,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
12881315
}
12891316
}
12901317
break;
1291-
case QVariant::StringList:
1318+
case QMetaType::QStringList:
12921319
{
12931320
bool ok;
12941321
QStringList l = PyObjToStringList(val, false, ok);
@@ -1308,7 +1335,11 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
13081335
// Try to convert the object to a QVariant based on the typeName
13091336
bool ok;
13101337
bool isPtr = false;
1338+
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
1339+
QByteArray typeName = QMetaType(type).name();
1340+
#else
13111341
QByteArray typeName = QMetaType::typeName(type);
1342+
#endif
13121343
if (typeName.endsWith("*")) {
13131344
isPtr = true;
13141345
typeName.truncate(typeName.length() - 1);
@@ -1323,7 +1354,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
13231354
}
13241355
}
13251356
}
1326-
} else if (static_cast<std::uint32_t>(type) >= QVariant::UserType) {
1357+
} else if (static_cast<std::uint32_t>(type) >= QMetaType::User) {
13271358
// not an instance wrapper, but there might be other converters
13281359
// Maybe we have a special converter that is registered for that type:
13291360
PythonQtConvertPythonToMetaTypeCB* converter = _pythonToMetaTypeConverters.value(type);
@@ -1505,66 +1536,66 @@ bool PythonQtConv::ConvertPythonListToQListOfPointerType(PyObject* obj, QList<vo
15051536
QString PythonQtConv::CPPObjectToString(int type, const void* data) {
15061537
QString r;
15071538
switch (type) {
1508-
case QVariant::Size: {
1539+
case QMetaType::QSize: {
15091540
const QSize* s = static_cast<const QSize*>(data);
15101541
r = QString::number(s->width()) + ", " + QString::number(s->height());
15111542
}
15121543
break;
1513-
case QVariant::SizeF: {
1544+
case QMetaType::QSizeF: {
15141545
const QSizeF* s = static_cast<const QSizeF*>(data);
15151546
r = QString::number(s->width()) + ", " + QString::number(s->height());
15161547
}
15171548
break;
1518-
case QVariant::Point: {
1549+
case QMetaType::QPoint: {
15191550
const QPoint* s = static_cast<const QPoint*>(data);
15201551
r = QString::number(s->x()) + ", " + QString::number(s->y());
15211552
}
15221553
break;
1523-
case QVariant::PointF: {
1554+
case QMetaType::QPointF: {
15241555
const QPointF* s = static_cast<const QPointF*>(data);
15251556
r = QString::number(s->x()) + ", " + QString::number(s->y());
15261557
}
15271558
break;
1528-
case QVariant::Rect: {
1559+
case QMetaType::QRect: {
15291560
const QRect* s = static_cast<const QRect*>(data);
15301561
r = QString::number(s->x()) + ", " + QString::number(s->y());
15311562
r += ", " + QString::number(s->width()) + ", " + QString::number(s->height());
15321563
}
15331564
break;
1534-
case QVariant::RectF: {
1565+
case QMetaType::QRectF: {
15351566
const QRectF* s = static_cast<const QRectF*>(data);
15361567
r = QString::number(s->x()) + ", " + QString::number(s->y());
15371568
r += ", " + QString::number(s->width()) + ", " + QString::number(s->height());
15381569
}
15391570
break;
1540-
case QVariant::Date: {
1571+
case QMetaType::QDate: {
15411572
const QDate* s = static_cast<const QDate*>(data);
15421573
r = s->toString(Qt::ISODate);
15431574
}
15441575
break;
1545-
case QVariant::DateTime: {
1576+
case QMetaType::QDateTime: {
15461577
const QDateTime* s = static_cast<const QDateTime*>(data);
15471578
r = s->toString(Qt::ISODate);
15481579
}
15491580
break;
1550-
case QVariant::Time: {
1581+
case QMetaType::QTime: {
15511582
const QTime* s = static_cast<const QTime*>(data);
15521583
r = s->toString(Qt::ISODate);
15531584
}
15541585
break;
1555-
case QVariant::Pixmap:
1586+
case QMetaType::QPixmap:
15561587
{
15571588
const QPixmap* s = static_cast<const QPixmap*>(data);
15581589
r = QString("Pixmap ") + QString::number(s->width()) + ", " + QString::number(s->height());
15591590
}
15601591
break;
1561-
case QVariant::Image:
1592+
case QMetaType::QImage:
15621593
{
15631594
const QImage* s = static_cast<const QImage*>(data);
15641595
r = QString("Image ") + QString::number(s->width()) + ", " + QString::number(s->height());
15651596
}
15661597
break;
1567-
case QVariant::Url:
1598+
case QMetaType::QUrl:
15681599
{
15691600
const QUrl* s = static_cast<const QUrl*>(data);
15701601
r = s->toString();
@@ -1574,7 +1605,7 @@ QString PythonQtConv::CPPObjectToString(int type, const void* data) {
15741605
default:
15751606
// this creates a copy, but that should not be expensive for typical simple variants
15761607
// (but we do not want to do this for our own user types!)
1577-
if (type>0 && type < (int)QVariant::UserType) {
1608+
if (type>0 && type < (int)QMetaType::User) {
15781609
r = variantFromType(type, data).toString();
15791610
}
15801611
}
@@ -1584,13 +1615,19 @@ QString PythonQtConv::CPPObjectToString(int type, const void* data) {
15841615
PyObject* PythonQtConv::createCopyFromMetaType( int type, const void* data )
15851616
{
15861617
// if the type is known, we can construct it via QMetaType::construct
1587-
#if( QT_VERSION >= QT_VERSION_CHECK(5,0,0) )
1618+
#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
1619+
void* newCPPObject = QMetaType(type).create(data);
1620+
// XXX this could be optimized by using metatypeid directly
1621+
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newCPPObject, QMetaType(type).name());
1622+
#elif QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
15881623
void* newCPPObject = QMetaType::create(type, data);
1624+
// XXX this could be optimized by using metatypeid directly
1625+
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newCPPObject, QMetaType::typeName(type));
15891626
#else
15901627
void* newCPPObject = QMetaType::construct(type, data);
1591-
#endif
15921628
// XXX this could be optimized by using metatypeid directly
15931629
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newCPPObject, QMetaType::typeName(type));
1630+
#endif
15941631
wrap->_ownedByPythonQt = true;
15951632
wrap->_useQMetaTypeDestroy = true;
15961633
return (PyObject*)wrap;

0 commit comments

Comments
 (0)