Skip to content

Commit 1bc682e

Browse files
committed
BUG: Remove deprecated Qt6 paradigms
Replace with backward compatible recommended or preprocessor conditional for old and new. Use new paradigms that are backward compatible when possible. Use preprocessor conditionals where the syntax is different between qt5 and qt6.
1 parent 72bcdc4 commit 1bc682e

File tree

6 files changed

+203
-50
lines changed

6 files changed

+203
-50
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: 81 additions & 45 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();
@@ -310,11 +314,17 @@ PyObject* PythonQtConv::convertQtValueToPythonInternal(int type, const void* dat
310314
void* PythonQtConv::handlePythonToQtAutoConversion(int typeId, PyObject* obj, void* alreadyAllocatedCPPObject, PythonQtArgumentFrame* frame)
311315
{
312316
void* ptr = alreadyAllocatedCPPObject;
313-
314-
static int penId = QMetaType::type("QPen");
315-
static int brushId = QMetaType::type("QBrush");
317+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
318+
static int penId = QMetaType::fromName("QPen").id();
319+
static int brushId = QMetaType::fromName("QBrush").id();
320+
static int cursorId = QMetaType::fromName("QCursor").id();
321+
static int colorId = QMetaType::fromName("QColor").id();
322+
#else
323+
static int penId = QMetaType::type("QPen");
324+
static int brushId = QMetaType::type("QBrush");
316325
static int cursorId = QMetaType::type("QCursor");
317-
static int colorId = QMetaType::type("QColor");
326+
static int colorId = QMetaType::type("QColor");
327+
#endif
318328
static PyObject* qtGlobalColorEnum = PythonQtClassInfo::findEnumWrapper("Qt::GlobalColor", nullptr);
319329
if (typeId == cursorId) {
320330
static PyObject* qtCursorShapeEnum = PythonQtClassInfo::findEnumWrapper("Qt::CursorShape", nullptr);
@@ -728,7 +738,11 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
728738
if (info.typeId == PythonQtMethodInfo::Unknown || info.typeId >= QMetaType::User) {
729739
// check for QList<AnyPtr*> case, where we will use a QList<void*> QVariant
730740
if (info.isQList && (info.innerNamePointerCount == 1)) {
741+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
742+
static int id = QMetaType::fromName("QList<void*>").id();
743+
#else
731744
static int id = QMetaType::type("QList<void*>");
745+
#endif
732746
if (!alreadyAllocatedCPPObject) {
733747
PythonQtArgumentFrame_ADD_VARIANT_VALUE_BY_ID_IF_NEEDED(alreadyAllocatedCPPObject, frame, id, ptr);
734748
ptr = (void*)((QVariant*)ptr)->constData();
@@ -1094,35 +1108,39 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
10941108
) {
10951109
// no special type requested
10961110
if (val == nullptr) {
1097-
type = QVariant::Invalid;
1111+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1112+
type = QMetaType::UnknownType;
1113+
#else
1114+
type = 0; // Equivalent to QVariant::Invalid or unregistered type
1115+
#endif
10981116
} else if (PyBytes_Check(val)) {
10991117
#ifdef PY3K
11001118
// In Python 3, it is a ByteArray
1101-
type = QVariant::ByteArray;
1119+
type = QMetaType::QByteArray;
11021120
#else
11031121
// In Python 2, we need to use String, since it might be a string
1104-
type = QVariant::String;
1122+
type = QMetaType::QString;
11051123
#endif
11061124
} else if (PyUnicode_Check(val)) {
1107-
type = QVariant::String;
1125+
type = QMetaType::QString;
11081126
} else if (val == Py_False || val == Py_True) {
1109-
type = QVariant::Bool;
1127+
type = QMetaType::Bool;
11101128
#ifndef PY3K
11111129
} else if (PyObject_TypeCheck(val, &PyInt_Type)) {
1112-
type = QVariant::Int;
1130+
type = QMetaType::Int;
11131131
#endif
11141132
} else if (PyLong_Check(val)) {
11151133
// return int if the value fits into that range,
11161134
// otherwise it would not be possible to get an int from Python 3
11171135
qint64 d = PyLong_AsLongLong(val);
11181136
if (d > std::numeric_limits<int>::max() ||
11191137
d < std::numeric_limits<int>::min()) {
1120-
type = QVariant::LongLong;
1138+
type = QMetaType::LongLong;
11211139
} else {
1122-
type = QVariant::Int;
1140+
type = QMetaType::Int;
11231141
}
11241142
} else if (PyFloat_Check(val)) {
1125-
type = QVariant::Double;
1143+
type = QMetaType::Double;
11261144
} else if (PyObject_TypeCheck(val, &PythonQtInstanceWrapper_Type)) {
11271145
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)val;
11281146
// c++ wrapper, check if the class names of the c++ objects match
@@ -1144,11 +1162,15 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
11441162
return v;
11451163
} else if (val == Py_None) {
11461164
// none is invalid
1147-
type = QVariant::Invalid;
1165+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1166+
type = QMetaType::UnknownType;
1167+
#else
1168+
type = 0; // Equivalent to QVariant::Invalid or unregistered type
1169+
#endif
11481170
} else if (PyDict_Check(val)) {
1149-
type = QVariant::Map;
1171+
type = QMetaType::QVariantMap;
11501172
} else if (PyList_Check(val) || PyTuple_Check(val) || PySequence_Check(val)) {
1151-
type = QVariant::List;
1173+
type = QMetaType::QVariantList;
11521174
} else {
11531175
// transport the Python objects directly inside of QVariant:
11541176
v = PythonQtObjectPtr(val).toVariant();
@@ -1157,28 +1179,32 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
11571179
}
11581180
// special type request:
11591181
switch (type) {
1160-
case QVariant::Invalid:
1182+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1183+
case QMetaType::UnknownType:
1184+
#else
1185+
case 0: // Equivalent to QVariant::Invalid or unregistered type
1186+
#endif
11611187
return v;
11621188
break;
1163-
case QVariant::Int:
1189+
case QMetaType::Int:
11641190
{
11651191
int d = PyObjGetInt(val, false, ok);
11661192
if (ok) return QVariant(d);
11671193
}
11681194
break;
1169-
case QVariant::UInt:
1195+
case QMetaType::UInt:
11701196
{
11711197
int d = PyObjGetInt(val, false,ok);
11721198
if (ok) v = QVariant((unsigned int)d);
11731199
}
11741200
break;
1175-
case QVariant::Bool:
1201+
case QMetaType::Bool:
11761202
{
11771203
int d = PyObjGetBool(val,false,ok);
11781204
if (ok) v = QVariant((bool)(d!=0));
11791205
}
11801206
break;
1181-
case QVariant::Double:
1207+
case QMetaType::Double:
11821208
{
11831209
double d = PyObjGetDouble(val,false,ok);
11841210
if (ok) v = QVariant(d);
@@ -1239,7 +1265,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
12391265
}
12401266
break;
12411267

1242-
case QVariant::ByteArray:
1268+
case QMetaType::QByteArray:
12431269
{
12441270
bool ok;
12451271
#ifdef PY3K
@@ -1249,20 +1275,20 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
12491275
#endif
12501276
}
12511277
break;
1252-
case QVariant::String:
1278+
case QMetaType::QString:
12531279
{
12541280
bool ok;
12551281
v = QVariant(PyObjGetString(val, false, ok));
12561282
}
12571283
break;
12581284

1259-
case QVariant::Map:
1285+
case QMetaType::QVariantMap:
12601286
pythonToMapVariant<QVariantMap>(val, v);
12611287
break;
1262-
case QVariant::Hash:
1288+
case QMetaType::QVariantHash:
12631289
pythonToMapVariant<QVariantHash>(val, v);
12641290
break;
1265-
case QVariant::List:
1291+
case QMetaType::QVariantList:
12661292
{
12671293
bool isListOrTuple = PyList_Check(val) || PyTuple_Check(val);
12681294
if (isListOrTuple || PySequence_Check(val)) {
@@ -1288,7 +1314,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
12881314
}
12891315
}
12901316
break;
1291-
case QVariant::StringList:
1317+
case QMetaType::QStringList:
12921318
{
12931319
bool ok;
12941320
QStringList l = PyObjToStringList(val, false, ok);
@@ -1308,7 +1334,11 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
13081334
// Try to convert the object to a QVariant based on the typeName
13091335
bool ok;
13101336
bool isPtr = false;
1337+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1338+
QByteArray typeName = QMetaType(type).name();
1339+
#else
13111340
QByteArray typeName = QMetaType::typeName(type);
1341+
#endif
13121342
if (typeName.endsWith("*")) {
13131343
isPtr = true;
13141344
typeName.truncate(typeName.length() - 1);
@@ -1323,7 +1353,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
13231353
}
13241354
}
13251355
}
1326-
} else if (static_cast<std::uint32_t>(type) >= QVariant::UserType) {
1356+
} else if (static_cast<std::uint32_t>(type) >= QMetaType::User) {
13271357
// not an instance wrapper, but there might be other converters
13281358
// Maybe we have a special converter that is registered for that type:
13291359
PythonQtConvertPythonToMetaTypeCB* converter = _pythonToMetaTypeConverters.value(type);
@@ -1504,66 +1534,66 @@ bool PythonQtConv::ConvertPythonListToQListOfPointerType(PyObject* obj, QList<vo
15041534
QString PythonQtConv::CPPObjectToString(int type, const void* data) {
15051535
QString r;
15061536
switch (type) {
1507-
case QVariant::Size: {
1537+
case QMetaType::QSize: {
15081538
const QSize* s = static_cast<const QSize*>(data);
15091539
r = QString::number(s->width()) + ", " + QString::number(s->height());
15101540
}
15111541
break;
1512-
case QVariant::SizeF: {
1542+
case QMetaType::QSizeF: {
15131543
const QSizeF* s = static_cast<const QSizeF*>(data);
15141544
r = QString::number(s->width()) + ", " + QString::number(s->height());
15151545
}
15161546
break;
1517-
case QVariant::Point: {
1547+
case QMetaType::QPoint: {
15181548
const QPoint* s = static_cast<const QPoint*>(data);
15191549
r = QString::number(s->x()) + ", " + QString::number(s->y());
15201550
}
15211551
break;
1522-
case QVariant::PointF: {
1552+
case QMetaType::QPointF: {
15231553
const QPointF* s = static_cast<const QPointF*>(data);
15241554
r = QString::number(s->x()) + ", " + QString::number(s->y());
15251555
}
15261556
break;
1527-
case QVariant::Rect: {
1557+
case QMetaType::QRect: {
15281558
const QRect* s = static_cast<const QRect*>(data);
15291559
r = QString::number(s->x()) + ", " + QString::number(s->y());
15301560
r += ", " + QString::number(s->width()) + ", " + QString::number(s->height());
15311561
}
15321562
break;
1533-
case QVariant::RectF: {
1563+
case QMetaType::QRectF: {
15341564
const QRectF* s = static_cast<const QRectF*>(data);
15351565
r = QString::number(s->x()) + ", " + QString::number(s->y());
15361566
r += ", " + QString::number(s->width()) + ", " + QString::number(s->height());
15371567
}
15381568
break;
1539-
case QVariant::Date: {
1569+
case QMetaType::QDate: {
15401570
const QDate* s = static_cast<const QDate*>(data);
15411571
r = s->toString(Qt::ISODate);
15421572
}
15431573
break;
1544-
case QVariant::DateTime: {
1574+
case QMetaType::QDateTime: {
15451575
const QDateTime* s = static_cast<const QDateTime*>(data);
15461576
r = s->toString(Qt::ISODate);
15471577
}
15481578
break;
1549-
case QVariant::Time: {
1579+
case QMetaType::QTime: {
15501580
const QTime* s = static_cast<const QTime*>(data);
15511581
r = s->toString(Qt::ISODate);
15521582
}
15531583
break;
1554-
case QVariant::Pixmap:
1584+
case QMetaType::QPixmap:
15551585
{
15561586
const QPixmap* s = static_cast<const QPixmap*>(data);
15571587
r = QString("Pixmap ") + QString::number(s->width()) + ", " + QString::number(s->height());
15581588
}
15591589
break;
1560-
case QVariant::Image:
1590+
case QMetaType::QImage:
15611591
{
15621592
const QImage* s = static_cast<const QImage*>(data);
15631593
r = QString("Image ") + QString::number(s->width()) + ", " + QString::number(s->height());
15641594
}
15651595
break;
1566-
case QVariant::Url:
1596+
case QMetaType::QUrl:
15671597
{
15681598
const QUrl* s = static_cast<const QUrl*>(data);
15691599
r = s->toString();
@@ -1573,7 +1603,7 @@ QString PythonQtConv::CPPObjectToString(int type, const void* data) {
15731603
default:
15741604
// this creates a copy, but that should not be expensive for typical simple variants
15751605
// (but we do not want to do this for our own user types!)
1576-
if (type>0 && type < (int)QVariant::UserType) {
1606+
if (type>0 && type < (int)QMetaType::User) {
15771607
r = variantFromType(type, data).toString();
15781608
}
15791609
}
@@ -1583,13 +1613,19 @@ QString PythonQtConv::CPPObjectToString(int type, const void* data) {
15831613
PyObject* PythonQtConv::createCopyFromMetaType( int type, const void* data )
15841614
{
15851615
// if the type is known, we can construct it via QMetaType::construct
1586-
#if( QT_VERSION >= QT_VERSION_CHECK(5,0,0) )
1587-
void* newCPPObject = QMetaType::create(type, data);
1616+
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
1617+
void* newCPPObject = QMetaType(type).create(data);
1618+
// XXX this could be optimized by using metatypeid directly
1619+
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newCPPObject, QMetaType(type).name());
1620+
#elif QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
1621+
void* newCPPObject = QMetaType::create(type, data);
1622+
// XXX this could be optimized by using metatypeid directly
1623+
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newCPPObject, QMetaType::typeName(type));
15881624
#else
1589-
void* newCPPObject = QMetaType::construct(type, data);
1590-
#endif
1625+
void* newCPPObject = QMetaType::construct(type, data);
15911626
// XXX this could be optimized by using metatypeid directly
15921627
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newCPPObject, QMetaType::typeName(type));
1628+
#endif
15931629
wrap->_ownedByPythonQt = true;
15941630
wrap->_useQMetaTypeDestroy = true;
15951631
return (PyObject*)wrap;

0 commit comments

Comments
 (0)