Skip to content
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

Qt6 compatibility fixes for core PythonQt library #109

Merged
Merged
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
5 changes: 1 addition & 4 deletions src/PythonQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
} else {
qRegisterMetaType<quint32>("size_t");
}
int stringRefId = qRegisterMetaType<QStringRef>("QStringRef");
PythonQtConv::registerMetaTypeToPythonConverter(stringRefId, PythonQtConv::convertFromStringRef);

int objectPtrListId = qRegisterMetaType<QList<PythonQtObjectPtr> >("QList<PythonQtObjectPtr>");
PythonQtConv::registerMetaTypeToPythonConverter(objectPtrListId, PythonQtConv::convertFromQListOfPythonQtObjectPtr);
Expand Down Expand Up @@ -239,7 +237,7 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QLineF);
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QPoint);
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QPointF);
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QRegExp);
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QRegularExpression);

PythonQtRegisterToolClassesTemplateConverterForKnownClass(QFont);
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QPixmap);
Expand All @@ -257,7 +255,6 @@ void PythonQt::init(int flags, const QByteArray& pythonQtModuleName)
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QPen);
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QTextLength);
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QTextFormat);
PythonQtRegisterToolClassesTemplateConverterForKnownClass(QMatrix);

PyObject* pack = PythonQt::priv()->packageByName("QtCore");
PyObject* pack2 = PythonQt::priv()->packageByName("Qt");
Expand Down
3 changes: 2 additions & 1 deletion src/PythonQtClassInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ QStringList PythonQtClassInfo::memberList()
}
}

return QSet<QString>::fromList(l).toList();
QSet<QString> set(l.begin(), l.end());
return set.values();
}

const QByteArray& PythonQtClassInfo::className() const
Expand Down
4 changes: 2 additions & 2 deletions src/PythonQtClassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,8 @@ static PyObject *PythonQtClassWrapper_getattro(PyObject *obj, PyObject *name)
}
PyObject* dict = PyDict_New();

QSet<QString> completeSet = QSet<QString>::fromList(wrapper->classInfo()->memberList());
completeSet.unite(QSet<QString>::fromList(wrapper->classInfo()->propertyList()));
QSet<QString> completeSet(wrapper->classInfo()->memberList().begin(), wrapper->classInfo()->memberList().end());
Copy link
Contributor

Choose a reason for hiding this comment

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

Both memberList() and propertyList() return a new QStringList object; it isn't a valid use to use begin() and end() from different list instances, even if it seems to work for you.
Assign the result of memberList() and propertyList() to a local variable and use begin() and end() on that.

completeSet.unite(QSet<QString>(wrapper->classInfo()->propertyList().begin(), wrapper->classInfo()->propertyList().end()));

Q_FOREACH (QString name, completeSet) {
if (name.startsWith("py_")) {
Expand Down
41 changes: 18 additions & 23 deletions src/PythonQtConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1015,17 +1015,17 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
if (wrap->classInfo()->isCPPWrapper()) {
if (wrap->classInfo()->metaTypeId()>0) {
// construct a new variant from the C++ object if it has a meta type (this will COPY the object!)
v = QVariant(wrap->classInfo()->metaTypeId(), wrap->_wrappedPtr);
v = QVariant(QMetaType(wrap->classInfo()->metaTypeId()), wrap->_wrappedPtr);
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a note: It seems this constructor of QVariant only exists for Qt6, so we can't merge this as is into master, as this would break compatibility with Qt5.
I'd prefer to have the same code of the PythonQt library itself for Qt5 and Qt6 (the binding generator is a completely different thing), so we will need a few #if QT_VERSION >= 0x060000 statements. And we will need to decide which Qt5 versions we still want to support.

} else {
// TODOXXX we could as well check if there is a registered meta type for "classname*", so that we may pass
// the pointer here...
// is this worth anything? we loose the knowledge of the cpp object type
v = qVariantFromValue(wrap->_wrappedPtr);
v = QVariant::fromValue(wrap->_wrappedPtr);
}
} else {
// this gives us a QObject pointer
QObject* myObject = wrap->_obj;
v = qVariantFromValue(myObject);
v = QVariant::fromValue(myObject);
}
return v;
} else if (val == Py_None) {
Expand Down Expand Up @@ -1073,55 +1073,55 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
case QMetaType::Float:
{
float d = (float) PyObjGetDouble(val,false,ok);
if (ok) v = qVariantFromValue(d);
if (ok) v = QVariant::fromValue(d);
}
break;
case QMetaType::Long:
{
long d = (long) PyObjGetLongLong(val,false,ok);
if (ok) v = qVariantFromValue(d);
if (ok) v = QVariant::fromValue(d);
}
break;
case QMetaType::ULong:
{
unsigned long d = (unsigned long) PyObjGetLongLong(val,false,ok);
if (ok) v = qVariantFromValue(d);
if (ok) v = QVariant::fromValue(d);
}
break;
case QMetaType::LongLong:
{
qint64 d = PyObjGetLongLong(val, false, ok);
if (ok) v = qVariantFromValue(d);
if (ok) v = QVariant::fromValue(d);
}
break;
case QMetaType::ULongLong:
{
quint64 d = PyObjGetULongLong(val, false, ok);
if (ok) v = qVariantFromValue(d);
if (ok) v = QVariant::fromValue(d);
}
break;
case QMetaType::Short:
{
short d = (short) PyObjGetInt(val,false,ok);
if (ok) v = qVariantFromValue(d);
if (ok) v = QVariant::fromValue(d);
}
break;
case QMetaType::UShort:
{
unsigned short d = (unsigned short) PyObjGetInt(val,false,ok);
if (ok) v = qVariantFromValue(d);
if (ok) v = QVariant::fromValue(d);
}
break;
case QMetaType::Char:
{
char d = (char) PyObjGetInt(val,false,ok);
if (ok) v = qVariantFromValue(d);
if (ok) v = QVariant::fromValue(d);
}
break;
case QMetaType::UChar:
{
unsigned char d = (unsigned char) PyObjGetInt(val,false,ok);
if (ok) v = qVariantFromValue(d);
if (ok) v = QVariant::fromValue(d);
}
break;

Expand Down Expand Up @@ -1189,7 +1189,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
PythonQtInstanceWrapper* wrap = (PythonQtInstanceWrapper*)val;
if (wrap->classInfo()->isCPPWrapper() && wrap->classInfo()->metaTypeId() == type) {
// construct a new variant from the C++ object if it has the same meta type
v = QVariant(type, wrap->_wrappedPtr);
v = QVariant(QMetaType(type), wrap->_wrappedPtr);
} else {
// Try to convert the object to a QVariant based on the typeName
bool ok;
Expand All @@ -1202,10 +1202,10 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
void* object = castWrapperTo(wrap, typeName, ok);
if (ok) {
if (isPtr) {
v = QVariant(type, &object);
v = QVariant(QMetaType(type), &object);
}
else {
v = QVariant(type, object);
v = QVariant(QMetaType(type), object);
}
}
}
Expand All @@ -1215,7 +1215,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
PythonQtConvertPythonToMetaTypeCB* converter = _pythonToMetaTypeConverters.value(type);
if (converter) {
// allocate a default object of the needed type:
v = QVariant(type, (const void*)nullptr);
v = QVariant(QMetaType(type), (const void*)nullptr);
// now call the converter, passing the internal object of the variant
ok = (*converter)(val, (void*)v.constData(), type, true);
if (!ok) {
Expand All @@ -1226,7 +1226,7 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
const PythonQtMethodInfo::ParameterInfo& info = PythonQtMethodInfo::getParameterInfoForMetaType(type);
if (info.isQList && (info.innerNamePointerCount == 1)) {
// allocate a default object of the needed type:
v = QVariant(type, (const void*)nullptr);
v = QVariant(QMetaType(type), (const void*)nullptr);
ok = ConvertPythonListToQListOfPointerType(val, (QList<void*>*)v.constData(), info, true);
if (!ok) {
v = QVariant();
Expand Down Expand Up @@ -1461,7 +1461,7 @@ QString PythonQtConv::CPPObjectToString(int type, const void* data) {
// this creates a copy, but that should not be expensive for typical simple variants
// (but we do not want to do this for our won user types!
if (type>0 && type < (int)QVariant::UserType) {
QVariant v(type, data);
QVariant v(QMetaType(type), data);
r = v.toString();
}
}
Expand All @@ -1483,11 +1483,6 @@ PyObject* PythonQtConv::createCopyFromMetaType( int type, const void* data )
return (PyObject*)wrap;
}

PyObject* PythonQtConv::convertFromStringRef(const void* inObject, int /*metaTypeId*/)
{
return PythonQtConv::QStringToPyObject(((QStringRef*)inObject)->toString());
}

QByteArray PythonQtConv::getCPPTypeName(PyObject* type)
{
QByteArray result;
Expand Down
1 change: 0 additions & 1 deletion src/PythonQtConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ class PYTHONQT_EXPORT PythonQtConv {
static PyObject* convertFromPythonQtSafeObjectPtr(const void* /* PythonQtObjectPtr* */ inObject, int /*metaTypeId*/);
static bool convertToQListOfPythonQtObjectPtr(PyObject* obj, void* /* QList<PythonQtObjectPtr>* */ outList, int /*metaTypeId*/, bool /*strict*/);
static PyObject* convertFromQListOfPythonQtObjectPtr(const void* /* QList<PythonQtObjectPtr>* */ inObject, int /*metaTypeId*/);
static PyObject* convertFromStringRef(const void* inObject, int /*metaTypeId*/);

//! Returns the name of the equivalent CPP type (for signals and slots)
static QByteArray getCPPTypeName(PyObject* type);
Expand Down
8 changes: 4 additions & 4 deletions src/PythonQtImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args)
{
PythonQtImporter *self = (PythonQtImporter *)obj;
PyObject *code = nullptr, *mod = nullptr, *dict = nullptr;
char *fullname;
char *fullname = NULL;

if (!PyArg_ParseTuple(args, "s:PythonQtImporter.load_module",
&fullname))
Expand Down Expand Up @@ -338,7 +338,7 @@ PythonQtImporter_load_module(PyObject *obj, PyObject *args)
QVariantList list = result.toList();
if (list.count()==3) {
// We prepend the full module name (including package prefix)
list.prepend(fullname);
list.prepend(QString(fullname));
#ifdef __linux
#ifdef _DEBUG
// imp_find_module() does not respect the debug suffix '_d' on Linux,
Expand Down Expand Up @@ -740,7 +740,7 @@ PythonQtImport::getCodeFromData(const QString& path, int isbytecode,int /*ispack
QDateTime time;
time = PythonQt::importInterface()->lastModifiedDate(path);
QString cacheFilename = getCacheFilename(path, /*isOptimizedFilename=*/false);
writeCompiledModule((PyCodeObject*)code, cacheFilename, time.toTime_t(), /*sourceSize=*/qdata.length());
writeCompiledModule((PyCodeObject*)code, cacheFilename, time.toSecsSinceEpoch(), /*sourceSize=*/qdata.length());
}
}
return code;
Expand All @@ -754,7 +754,7 @@ PythonQtImport::getMTimeOfSource(const QString& path)
if (PythonQt::importInterface()->exists(path2)) {
QDateTime t = PythonQt::importInterface()->lastModifiedDate(path2);
if (t.isValid()) {
mtime = t.toTime_t();
mtime = t.toSecsSinceEpoch();
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/PythonQtMethodInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ int PythonQtMethodInfo::nameToType(const char* name)
_parameterTypeDict.insert("QLineF", QMetaType::QLineF);
_parameterTypeDict.insert("QPoint", QMetaType::QPoint);
_parameterTypeDict.insert("QPointF", QMetaType::QPointF);
_parameterTypeDict.insert("QRegExp", QMetaType::QRegExp);
_parameterTypeDict.insert("QRegularExpression", QMetaType::QRegularExpression);
_parameterTypeDict.insert("QFont", QMetaType::QFont);
_parameterTypeDict.insert("QPixmap", QMetaType::QPixmap);
_parameterTypeDict.insert("QBrush", QMetaType::QBrush);
Expand All @@ -388,7 +388,6 @@ int PythonQtMethodInfo::nameToType(const char* name)
_parameterTypeDict.insert("QPen", QMetaType::QPen);
_parameterTypeDict.insert("QTextLength", QMetaType::QTextLength);
_parameterTypeDict.insert("QTextFormat", QMetaType::QTextFormat);
_parameterTypeDict.insert("QMatrix", QMetaType::QMatrix);
_parameterTypeDict.insert("QVariant", PythonQtMethodInfo::Variant);
// own special types... (none so far, could be e.g. ObjectList
}
Expand Down
2 changes: 1 addition & 1 deletion src/PythonQtSlot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ meth_get__doc__(PythonQtSlotFunctionObject * m, void * /*closure*/)
if (!names.at(i - 1).isEmpty()) {
doc += names.at(i - 1);
} else {
doc += QString('a' + i - firstArgOffset).toLatin1();
doc += QString(QChar((char) ('a' + i - firstArgOffset))).toLatin1();
}
}
doc += ")";
Expand Down
8 changes: 5 additions & 3 deletions src/PythonQtStdDecorators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "PythonQtConversion.h"

#include <QCoreApplication>
#include <QRegularExpression>

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

QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QRegExp& regExp)
QList<QObject*> PythonQtStdDecorators::findChildren(QObject* parent, PyObject* type, const QRegularExpression& regExp)
{
const QMetaObject* meta = nullptr;
QByteArray typeName;
Expand Down Expand Up @@ -322,7 +323,7 @@ int PythonQtStdDecorators::findChildren(QObject* parent, const char* typeName, c
return 0;
}

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

// Skip if the name doesn't match.
if (regExp.indexIn(obj->objectName()) == -1)
QRegularExpressionMatch match = regExp.match(obj->objectName());
if (match.hasMatch() == false)
continue;

if ((typeName && obj->inherits(typeName)) ||
Expand Down
9 changes: 5 additions & 4 deletions src/PythonQtStdDecorators.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include <QMetaMethod>
#include <QMetaEnum>
#include <QMetaProperty>
#include <QRandomGenerator>

class PYTHONQT_EXPORT PythonQtStdDecorators : public QObject
{
Expand All @@ -82,7 +83,7 @@ public Q_SLOTS:
const QObjectList* children(QObject* o);
QObject* findChild(QObject* parent, PyObject* type, const QString& name = QString());
QList<QObject*> findChildren(QObject* parent, PyObject* type, const QString& name= QString());
QList<QObject*> findChildren(QObject* parent, PyObject* type, const QRegExp& regExp);
QList<QObject*> findChildren(QObject* parent, PyObject* type, const QRegularExpression& regExp);

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

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

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

class PythonQtSingleShotTimer : public QTimer
Expand Down
3 changes: 1 addition & 2 deletions src/PythonQtVariants.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
#include <QLineF>
#include <QPoint>
#include <QPointF>
#include <QRegExp>
#include <QRegularExpression>

#include <QFont>
#include <QBitmap>
Expand All @@ -72,7 +72,6 @@
#include <QPen>
#include <QTextLength>
#include <QTextFormat>
#include <QMatrix>

#endif

2 changes: 1 addition & 1 deletion src/gui/PythonQtScriptingConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class PYTHONQT_EXPORT PythonQtScriptingConsole : public QTextEdit
Q_OBJECT

public:
PythonQtScriptingConsole(QWidget* parent, const PythonQtObjectPtr& context, Qt::WindowFlags i = 0);
PythonQtScriptingConsole(QWidget* parent, const PythonQtObjectPtr& context, Qt::WindowFlags i = Qt::WindowFlags());

~PythonQtScriptingConsole() override;

Expand Down
8 changes: 6 additions & 2 deletions src/src.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# $Source$
# --------------------------------------------------

TARGET = PythonQt-Qt5-PythonXY
TARGET = PythonQt-Qt6-PythonXY
TEMPLATE = lib

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

DEFINES += PYTHONQT_CATCH_ALL_EXCEPTIONS

contains(QT_MAJOR_VERSION, 5) {
contains(QT_MAJOR_VERSION, 6) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: Perhaps use
contains(QT_MAJOR_VERSION, 5)|contains(QT_MAJOR_VERSION, 6)
when we achieve true compatibility with Qt5 and Qt6

Copy link
Contributor

Choose a reason for hiding this comment

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

As this is for Qt5 and 6 only, there is no need for the check at all.

QT += widgets core-private
}

INCLUDEPATH += $$PWD

macx {
QMAKE_APPLE_DEVICE_ARCHS = x86_64 arm64
}

include ( ../build/common.prf )
include ( ../build/python.prf )
TARGET = $$replace(TARGET, PythonXY, Python$${PYTHON_VERSION})
Expand Down
Loading