pythonqt
pythonqt copied to clipboard
Allow only scriptable methods and properties
What is the recommended way, to allow only scriptable methods (annotated with Q_SCRIPTABLE) and scriptable properties (SCRIPTABLE attribute of a Q_PROPERTY)?
Should I filter the data when doing introspection (PythonQt::introspection) to prevent the user from selecting any other methods and properties or is it possible to filter out methods and properties when a QObject becomes visible / registered to the Python interpreter. What is the right place in the source code to touch if i would like to enable only scriptable methods and properties?
Ok, I have found the solution. The relevant file is PythonQtClassInfo and the relevant methods ar propertyList and memberList. I added tests for scriptable attributes and this worked:
QStringList PythonQtClassInfo::propertyList()
{
QStringList l;
if (_isQObject && _meta) {
int i;
int numProperties = _meta->propertyCount();
for (i = 0; i < numProperties; i++) {
QMetaProperty p = _meta->property(i);
if (!p.isScriptable()) {
l << QString(p.name());
}
}
}
QStringList members = memberList();
foreach(QString member, members) {
if (member.startsWith("py_get_")) {
l << member.mid(7);
}
}
return l;
}
QStringList PythonQtClassInfo::memberList()
{
decorator();
QStringList l;
QString h;
// normal slots of QObject (or wrapper QObject)
if (_meta) {
int numMethods = _meta->methodCount();
bool skipQObj = !_isQObject;
for (int i = skipQObj?QObject::staticMetaObject.methodCount():0; i < numMethods; i++) {
QMetaMethod m = _meta->method(i);
if ((m.attributes() & QMetaMethod::Scriptable) &&
(((m.methodType() == QMetaMethod::Method || m.methodType() == QMetaMethod::Slot) && m.access() == QMetaMethod::Public)
|| m.methodType()==QMetaMethod::Signal))
{
l << PythonQtUtils::methodName(m);
}
}
}