aboutsummaryrefslogtreecommitdiffstats
path: root/libpyside
diff options
context:
space:
mode:
authorHugo Parente Lima <hugo.pl@gmail.com>2010-09-15 10:44:27 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2010-09-15 11:33:41 -0300
commit87ea5d920c62eed3e9c1185fa66740c13c71f65b (patch)
tree4653548a4b7050c85f481864c5c8d8d42bb4a975 /libpyside
parentcd7dac40f758f821514693c6d7d4c449a9f81240 (diff)
Fix bug#347 - "Setting properties in constructors gives incorrect results"
Added new function to libpyside: "fillQtProperties". Reviewer: Luciano Wolf <luciano.wolf@openbossa.org> Renato Araújo <renato.filho@openbossa.org>
Diffstat (limited to 'libpyside')
-rw-r--r--libpyside/pyside.cpp41
-rw-r--r--libpyside/pyside.h13
2 files changed, 54 insertions, 0 deletions
diff --git a/libpyside/pyside.cpp b/libpyside/pyside.cpp
index bfb9889b1..ddec19803 100644
--- a/libpyside/pyside.cpp
+++ b/libpyside/pyside.cpp
@@ -24,6 +24,10 @@
#include "pyside.h"
#include "signalmanager.h"
#include "qproperty.h"
+#include <basewrapper.h>
+#include <conversions.h>
+#include <algorithm>
+#include "qsignal.h"
extern "C" void init_signal(PyObject* module);
extern "C" void init_slot(PyObject* module);
@@ -41,5 +45,42 @@ void init(PyObject *module)
SignalManager::instance();
}
+bool fillQtProperties(PyObject* qObj, const QMetaObject* metaObj, PyObject* kwds, const char** blackList, unsigned int blackListSize)
+{
+
+ PyObject *key, *value;
+ Py_ssize_t pos = 0;
+
+ while (PyDict_Next(kwds, &pos, &key, &value)) {
+ if (!blackListSize || !std::binary_search(blackList, blackList + blackListSize, std::string(PyString_AS_STRING(key)))) {
+ QByteArray propName(PyString_AS_STRING(key));
+ if (metaObj->indexOfProperty(propName) != -1) {
+ propName[0] = std::toupper(propName[0]);
+ propName.prepend("set");
+
+ Shiboken::AutoDecRef propSetter(PyObject_GetAttrString(qObj, propName.constData()));
+ if (!propSetter.isNull()) {
+ Shiboken::AutoDecRef args(PyTuple_Pack(1, value));
+ Shiboken::AutoDecRef retval(PyObject_CallObject(propSetter, args));
+ } else {
+ PyObject* attr = PyObject_GenericGetAttr(qObj, key);
+ if (isQPropertyType(attr))
+ PySide::qproperty_set(attr, qObj, value);
+ }
+ } else {
+ propName.append("()");
+ if (metaObj->indexOfSignal(propName) != -1) {
+ propName.prepend('2');
+ PySide::signal_connect(qObj, propName, value);
+ } else {
+ PyErr_Format(PyExc_AttributeError, "'%s' is not a Qt property or a signal", propName.constData());
+ return false;
+ };
+ }
+ }
+ }
+ return true;
+}
+
} //namespace PySide
diff --git a/libpyside/pyside.h b/libpyside/pyside.h
index 21515c8d1..cae050b5d 100644
--- a/libpyside/pyside.h
+++ b/libpyside/pyside.h
@@ -26,6 +26,8 @@
#include <Python.h>
#include <pysidemacros.h>
#include <QMetaType>
+#include <QHash>
+#include <QList>
namespace PySide
{
@@ -42,6 +44,17 @@ inline uint hash(const T& value)
}
/**
+ * Fill QObject properties and do signal connections using the values found in \p kwds dictonary.
+ * \param qObj PyObject fot the QObject.
+ * \param metaObj QMetaObject of \p qObj.
+ * \param blackList keys to be ignored in kwds dictionary, this string list MUST be sorted.
+ * \param blackListSize numbe rof elements in blackList.
+ * \param kwds key->value dictonary.
+ * \return True if everything goes well, false with a Python error setted otherwise.
+ */
+PYSIDE_API bool fillQtProperties(PyObject* qObj, const QMetaObject* metaObj, PyObject* kwds, const char** blackList, unsigned int blackListSize);
+
+/**
* If the type \p T was registered on Qt meta type system with Q_DECLARE_METATYPE macro, this class will initialize
* the meta type.
*