Skip to content

BUG: Remove deprecated Qt6 paradigms #270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/PythonQtClassInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ void PythonQtClassInfo::setupCPPObject(const QByteArray& classname)
{
_isQObject = false;
_wrappedClassName = classname;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
_metaTypeId = QMetaType::fromName(classname).id();
#else
_metaTypeId = QMetaType::type(classname);
#endif
Comment on lines +105 to +109
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - could you move this into PythonQtUtils.h, together with the other version specific stuff? That would avoid cluttering the code with too many #ifdefs. Same for the other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand your request. I'm unsure how to move this definition from the source code to the header file while keeping the compilation unit separate from the declaration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean adding a convenience function (something like metaTypeIdFromClassName) that encapsulates the #ifdefs, like the rest of the functions in that namespace that are defined inline (I guess, for performance reasons).

if (_metaTypeId == 0) {
_metaTypeId = -1;
}
Expand Down
126 changes: 81 additions & 45 deletions src/PythonQtConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,11 @@ PyObject* PythonQtConv::convertQtValueToPythonInternal(int type, const void* dat
default:
// check if we have a QList of pointers, which we can circumvent with a QList<void*>
if (info.isQList && (info.innerNamePointerCount == 1)) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
static int id = QMetaType::fromName("QList<void*>").id();
#else
static int id = QMetaType::type("QList<void*>");
#endif
PythonQtArgumentFrame_ADD_VARIANT_VALUE_BY_ID(frame, id, ptr);
// return the constData pointer that will be filled with the result value later on
ptr = (void*)((QVariant*)ptr)->constData();
Expand Down Expand Up @@ -310,11 +314,17 @@ PyObject* PythonQtConv::convertQtValueToPythonInternal(int type, const void* dat
void* PythonQtConv::handlePythonToQtAutoConversion(int typeId, PyObject* obj, void* alreadyAllocatedCPPObject, PythonQtArgumentFrame* frame)
{
void* ptr = alreadyAllocatedCPPObject;

static int penId = QMetaType::type("QPen");
static int brushId = QMetaType::type("QBrush");
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
static int penId = QMetaType::fromName("QPen").id();
static int brushId = QMetaType::fromName("QBrush").id();
static int cursorId = QMetaType::fromName("QCursor").id();
static int colorId = QMetaType::fromName("QColor").id();
#else
static int penId = QMetaType::type("QPen");
static int brushId = QMetaType::type("QBrush");
static int cursorId = QMetaType::type("QCursor");
static int colorId = QMetaType::type("QColor");
static int colorId = QMetaType::type("QColor");
#endif
static PyObject* qtGlobalColorEnum = PythonQtClassInfo::findEnumWrapper("Qt::GlobalColor", nullptr);
if (typeId == cursorId) {
static PyObject* qtCursorShapeEnum = PythonQtClassInfo::findEnumWrapper("Qt::CursorShape", nullptr);
Expand Down Expand Up @@ -728,7 +738,11 @@ void* PythonQtConv::ConvertPythonToQt(const PythonQtMethodInfo::ParameterInfo& i
if (info.typeId == PythonQtMethodInfo::Unknown || info.typeId >= QMetaType::User) {
// check for QList<AnyPtr*> case, where we will use a QList<void*> QVariant
if (info.isQList && (info.innerNamePointerCount == 1)) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
static int id = QMetaType::fromName("QList<void*>").id();
#else
static int id = QMetaType::type("QList<void*>");
#endif
if (!alreadyAllocatedCPPObject) {
PythonQtArgumentFrame_ADD_VARIANT_VALUE_BY_ID_IF_NEEDED(alreadyAllocatedCPPObject, frame, id, ptr);
ptr = (void*)((QVariant*)ptr)->constData();
Expand Down Expand Up @@ -1094,35 +1108,39 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
) {
// no special type requested
if (val == nullptr) {
type = QVariant::Invalid;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
type = QMetaType::UnknownType;
#else
type = 0; // Equivalent to QVariant::Invalid or unregistered type
#endif
} else if (PyBytes_Check(val)) {
#ifdef PY3K
// In Python 3, it is a ByteArray
type = QVariant::ByteArray;
type = QMetaType::QByteArray;
#else
// In Python 2, we need to use String, since it might be a string
type = QVariant::String;
type = QMetaType::QString;
#endif
} else if (PyUnicode_Check(val)) {
type = QVariant::String;
type = QMetaType::QString;
} else if (val == Py_False || val == Py_True) {
type = QVariant::Bool;
type = QMetaType::Bool;
#ifndef PY3K
} else if (PyObject_TypeCheck(val, &PyInt_Type)) {
type = QVariant::Int;
type = QMetaType::Int;
#endif
} else if (PyLong_Check(val)) {
// return int if the value fits into that range,
// otherwise it would not be possible to get an int from Python 3
qint64 d = PyLong_AsLongLong(val);
if (d > std::numeric_limits<int>::max() ||
d < std::numeric_limits<int>::min()) {
type = QVariant::LongLong;
type = QMetaType::LongLong;
} else {
type = QVariant::Int;
type = QMetaType::Int;
}
} else if (PyFloat_Check(val)) {
type = QVariant::Double;
type = QMetaType::Double;
} else if (PyObject_TypeCheck(val, &PythonQtInstanceWrapper_Type)) {
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)val;
// c++ wrapper, check if the class names of the c++ objects match
Expand All @@ -1144,11 +1162,15 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
return v;
} else if (val == Py_None) {
// none is invalid
type = QVariant::Invalid;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
type = QMetaType::UnknownType;
#else
type = 0; // Equivalent to QVariant::Invalid or unregistered type
#endif
} else if (PyDict_Check(val)) {
type = QVariant::Map;
type = QMetaType::QVariantMap;
} else if (PyList_Check(val) || PyTuple_Check(val) || PySequence_Check(val)) {
type = QVariant::List;
type = QMetaType::QVariantList;
} else {
// transport the Python objects directly inside of QVariant:
v = PythonQtObjectPtr(val).toVariant();
Expand All @@ -1157,28 +1179,32 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
}
// special type request:
switch (type) {
case QVariant::Invalid:
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
case QMetaType::UnknownType:
#else
case 0: // Equivalent to QVariant::Invalid or unregistered type
#endif
return v;
break;
case QVariant::Int:
case QMetaType::Int:
{
int d = PyObjGetInt(val, false, ok);
if (ok) return QVariant(d);
}
break;
case QVariant::UInt:
case QMetaType::UInt:
{
int d = PyObjGetInt(val, false,ok);
if (ok) v = QVariant((unsigned int)d);
}
break;
case QVariant::Bool:
case QMetaType::Bool:
{
int d = PyObjGetBool(val,false,ok);
if (ok) v = QVariant((bool)(d!=0));
}
break;
case QVariant::Double:
case QMetaType::Double:
{
double d = PyObjGetDouble(val,false,ok);
if (ok) v = QVariant(d);
Expand Down Expand Up @@ -1239,7 +1265,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
}
break;

case QVariant::ByteArray:
case QMetaType::QByteArray:
{
bool ok;
#ifdef PY3K
Expand All @@ -1249,20 +1275,20 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
#endif
}
break;
case QVariant::String:
case QMetaType::QString:
{
bool ok;
v = QVariant(PyObjGetString(val, false, ok));
}
break;

case QVariant::Map:
case QMetaType::QVariantMap:
pythonToMapVariant<QVariantMap>(val, v);
break;
case QVariant::Hash:
case QMetaType::QVariantHash:
pythonToMapVariant<QVariantHash>(val, v);
break;
case QVariant::List:
case QMetaType::QVariantList:
{
bool isListOrTuple = PyList_Check(val) || PyTuple_Check(val);
if (isListOrTuple || PySequence_Check(val)) {
Expand All @@ -1288,7 +1314,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
}
}
break;
case QVariant::StringList:
case QMetaType::QStringList:
{
bool ok;
QStringList l = PyObjToStringList(val, false, ok);
Expand All @@ -1308,7 +1334,11 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
// Try to convert the object to a QVariant based on the typeName
bool ok;
bool isPtr = false;
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
QByteArray typeName = QMetaType(type).name();
#else
QByteArray typeName = QMetaType::typeName(type);
#endif
if (typeName.endsWith("*")) {
isPtr = true;
typeName.truncate(typeName.length() - 1);
Expand All @@ -1323,7 +1353,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
}
}
}
} else if (static_cast<std::uint32_t>(type) >= QVariant::UserType) {
} else if (static_cast<std::uint32_t>(type) >= QMetaType::User) {
// not an instance wrapper, but there might be other converters
// Maybe we have a special converter that is registered for that type:
PythonQtConvertPythonToMetaTypeCB* converter = _pythonToMetaTypeConverters.value(type);
Expand Down Expand Up @@ -1505,66 +1535,66 @@ bool PythonQtConv::ConvertPythonListToQListOfPointerType(PyObject* obj, QList<vo
QString PythonQtConv::CPPObjectToString(int type, const void* data) {
QString r;
switch (type) {
case QVariant::Size: {
case QMetaType::QSize: {
const QSize* s = static_cast<const QSize*>(data);
r = QString::number(s->width()) + ", " + QString::number(s->height());
}
break;
case QVariant::SizeF: {
case QMetaType::QSizeF: {
const QSizeF* s = static_cast<const QSizeF*>(data);
r = QString::number(s->width()) + ", " + QString::number(s->height());
}
break;
case QVariant::Point: {
case QMetaType::QPoint: {
const QPoint* s = static_cast<const QPoint*>(data);
r = QString::number(s->x()) + ", " + QString::number(s->y());
}
break;
case QVariant::PointF: {
case QMetaType::QPointF: {
const QPointF* s = static_cast<const QPointF*>(data);
r = QString::number(s->x()) + ", " + QString::number(s->y());
}
break;
case QVariant::Rect: {
case QMetaType::QRect: {
const QRect* s = static_cast<const QRect*>(data);
r = QString::number(s->x()) + ", " + QString::number(s->y());
r += ", " + QString::number(s->width()) + ", " + QString::number(s->height());
}
break;
case QVariant::RectF: {
case QMetaType::QRectF: {
const QRectF* s = static_cast<const QRectF*>(data);
r = QString::number(s->x()) + ", " + QString::number(s->y());
r += ", " + QString::number(s->width()) + ", " + QString::number(s->height());
}
break;
case QVariant::Date: {
case QMetaType::QDate: {
const QDate* s = static_cast<const QDate*>(data);
r = s->toString(Qt::ISODate);
}
break;
case QVariant::DateTime: {
case QMetaType::QDateTime: {
const QDateTime* s = static_cast<const QDateTime*>(data);
r = s->toString(Qt::ISODate);
}
break;
case QVariant::Time: {
case QMetaType::QTime: {
const QTime* s = static_cast<const QTime*>(data);
r = s->toString(Qt::ISODate);
}
break;
case QVariant::Pixmap:
case QMetaType::QPixmap:
{
const QPixmap* s = static_cast<const QPixmap*>(data);
r = QString("Pixmap ") + QString::number(s->width()) + ", " + QString::number(s->height());
}
break;
case QVariant::Image:
case QMetaType::QImage:
{
const QImage* s = static_cast<const QImage*>(data);
r = QString("Image ") + QString::number(s->width()) + ", " + QString::number(s->height());
}
break;
case QVariant::Url:
case QMetaType::QUrl:
{
const QUrl* s = static_cast<const QUrl*>(data);
r = s->toString();
Expand All @@ -1574,7 +1604,7 @@ QString PythonQtConv::CPPObjectToString(int type, const void* data) {
default:
// this creates a copy, but that should not be expensive for typical simple variants
// (but we do not want to do this for our own user types!)
if (type>0 && type < (int)QVariant::UserType) {
if (type>0 && type < (int)QMetaType::User) {
r = variantFromType(type, data).toString();
}
}
Expand All @@ -1584,13 +1614,19 @@ QString PythonQtConv::CPPObjectToString(int type, const void* data) {
PyObject* PythonQtConv::createCopyFromMetaType( int type, const void* data )
{
// if the type is known, we can construct it via QMetaType::construct
#if( QT_VERSION >= QT_VERSION_CHECK(5,0,0) )
void* newCPPObject = QMetaType::create(type, data);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
void* newCPPObject = QMetaType(type).create(data);
// XXX this could be optimized by using metatypeid directly
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newCPPObject, QMetaType(type).name());
#elif QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
void* newCPPObject = QMetaType::create(type, data);
// XXX this could be optimized by using metatypeid directly
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newCPPObject, QMetaType::typeName(type));
#else
void* newCPPObject = QMetaType::construct(type, data);
#endif
void* newCPPObject = QMetaType::construct(type, data);
// XXX this could be optimized by using metatypeid directly
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)PythonQt::priv()->wrapPtr(newCPPObject, QMetaType::typeName(type));
#endif
wrap->_ownedByPythonQt = true;
wrap->_useQMetaTypeDestroy = true;
return (PyObject*)wrap;
Expand Down
Loading
Loading