aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libpyside/pyside.cpp41
-rw-r--r--libpyside/pyside.h13
-rw-r--r--tests/QtCore/CMakeLists.txt1
-rw-r--r--tests/QtCore/setprop_on_ctor_test.py12
4 files changed, 67 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.
*
diff --git a/tests/QtCore/CMakeLists.txt b/tests/QtCore/CMakeLists.txt
index e0624e06f..76db54f25 100644
--- a/tests/QtCore/CMakeLists.txt
+++ b/tests/QtCore/CMakeLists.txt
@@ -60,6 +60,7 @@ PYSIDE_TEST(qtimer_singleshot_test.py)
PYSIDE_TEST(qtimer_timeout_test.py)
PYSIDE_TEST(qtnamespace_test.py)
PYSIDE_TEST(qurl_test.py)
+PYSIDE_TEST(setprop_on_ctor_test.py)
PYSIDE_TEST(static_method_test.py)
PYSIDE_TEST(static_protected_methods_test.py)
PYSIDE_TEST(thread_signals_test.py)
diff --git a/tests/QtCore/setprop_on_ctor_test.py b/tests/QtCore/setprop_on_ctor_test.py
new file mode 100644
index 000000000..bd4426cd1
--- /dev/null
+++ b/tests/QtCore/setprop_on_ctor_test.py
@@ -0,0 +1,12 @@
+#!/usr/bin/python
+import unittest
+from PySide.QtCore import *
+
+
+class SetPropOnCtorTest(unittest.TestCase):
+ def testIt(self):
+ obj = QEventTransition(targetStates = [QState()])
+ self.assertEqual(len(obj.targetStates()), 1);
+
+if __name__ == '__main__':
+ unittest.main()