Skip to content

Commit 74d323b

Browse files
author
florianlink
committed
improved PY3K conversions
fixed QProperty meta object init git-svn-id: http://svn.code.sf.net/p/pythonqt/code/trunk@434 ea8d5007-eb21-0410-b261-ccb3ea6e24a9
1 parent d5a327a commit 74d323b

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

src/PythonQt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,7 @@ void PythonQtPrivate::buildDynamicMetaObject(PythonQtClassWrapper* type, const Q
19771977
// Now look for slots: (this is a bug in QMetaObjectBuilder, all signals need to be added first)
19781978
while (PyDict_Next(dict, &pos, &key, &value)) {
19791979
if (PythonQtProperty_Check(value)) {
1980+
needsMetaObject = true;
19801981
PythonQtProperty* prop = (PythonQtProperty*)value;
19811982
QMetaPropertyBuilder newProp = builder.addProperty(PyString_AsString(key), prop->data->cppType);
19821983
newProp.setReadable(true);

src/PythonQtConversion.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,9 +746,13 @@ QString PythonQtConv::PyObjGetRepresentation(PyObject* val)
746746
QString PythonQtConv::PyObjGetString(PyObject* val, bool strict, bool& ok) {
747747
QString r;
748748
ok = true;
749+
#ifndef PY3K
750+
// in Python 3, we don't want to convert to QString, since we don't know anything about the encoding
749751
if (val->ob_type == &PyBytes_Type) {
750752
r = QString(PyBytes_AS_STRING(val));
751-
} else if (PyUnicode_Check(val)) {
753+
} else
754+
#endif
755+
if (PyUnicode_Check(val)) {
752756
#ifdef PY3K
753757
r = QString::fromUtf8(PyUnicode_AsUTF8(val));
754758
#else
@@ -976,8 +980,15 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
976980
#endif
977981
) {
978982
// no special type requested
979-
if (PyBytes_Check(val) || PyUnicode_Check(val)) {
980-
// NOTE: for compatibility reasons between Python 2/3 we don't use ByteArray for PyBytes_Type
983+
if (PyBytes_Check(val)) {
984+
#ifdef PY3K
985+
// In Python 3, it is a ByteArray
986+
type = QVariant::ByteArray;
987+
#else
988+
// In Python 2, we need to use String, since it might be a string
989+
type = QVariant::String;
990+
#endif
991+
} else if (PyUnicode_Check(val)) {
981992
type = QVariant::String;
982993
} else if (val == Py_False || val == Py_True) {
983994
type = QVariant::Bool;
@@ -1117,6 +1128,14 @@ QVariant PythonQtConv::PyObjToQVariant(PyObject* val, int type)
11171128
}
11181129

11191130
case QVariant::ByteArray:
1131+
{
1132+
bool ok;
1133+
#ifdef PY3K
1134+
v = QVariant(PyObjGetBytes(val, false, ok));
1135+
#else
1136+
v = QVariant(PyObjGetString(val, false, ok));
1137+
#endif
1138+
}
11201139
case QVariant::String:
11211140
{
11221141
bool ok;

src/PythonQtStdDecorators.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,12 @@ QVariant PythonQtStdDecorators::property(QObject* o, const char* name)
153153
return o->property(name);
154154
}
155155

156-
QString PythonQtStdDecorators::tr(QObject* obj, const QByteArray& text, const QByteArray& ambig, int n)
156+
QString PythonQtStdDecorators::tr(QObject* obj, const QString& text, const QString& ambig, int n)
157157
{
158158
#if( QT_VERSION >= QT_VERSION_CHECK(5,0,0) )
159-
return QCoreApplication::translate(obj->metaObject()->className(), text.constData(), ambig.constData(), n);
159+
return QCoreApplication::translate(obj->metaObject()->className(), text.toUtf8().constData(), ambig.toUtf8().constData(), n);
160160
#else
161-
return QCoreApplication::translate(obj->metaObject()->className(), text.constData(), ambig.constData(), QCoreApplication::CodecForTr, n);
161+
return QCoreApplication::translate(obj->metaObject()->className(), text.toUtf8().constData(), ambig.toUtf8().constData(), QCoreApplication::CodecForTr, n);
162162
#endif
163163
}
164164

src/PythonQtStdDecorators.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ public Q_SLOTS:
106106
int static_Qt_qrand() { return qrand(); }
107107
void static_Qt_qsrand(uint a) { qsrand(a); }
108108

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

111-
QByteArray static_Qt_SIGNAL(const QByteArray& s) { return QByteArray("2") + s; }
112-
QByteArray static_Qt_SLOT(const QByteArray& s) { return QByteArray("1") + s; }
111+
QString static_Qt_SIGNAL(const QString& s) { return QString("2") + s; }
112+
QString static_Qt_SLOT(const QString& s) { return QString("1") + s; }
113113

114114
void static_QTimer_singleShot(int msec, PyObject* callable);
115115

0 commit comments

Comments
 (0)