Sep-09-2018, 11:44 PM
I need to use a macro from the Qt framework in a class header (before __init__). In the example below, I would like to replace "obhub" by a variable, but I couldn't find how to do it. I need this to make a module and reuse the code more easily.
This is the working, static version:
This is the working, static version:
#!/usr/bin/python3 from PyQt5 import QtCore, QtDBus class QDBusObject(QtCore.QObject): def __init__(self, parent): QtCore.QObject.__init__(self) self.__dbusAdaptor = QDBusServerAdapter(self, parent) self.start() def start(self): bus = QtDBus.QDBusConnection.sessionBus() bus.registerObject("/org/obhub/session", self) bus.registerService("org.obhub.session") return bus class QDBusServerAdapter(QtDBus.QDBusAbstractAdaptor): QtCore.Q_CLASSINFO("D-Bus Interface", "org.obhub.session") QtCore.Q_CLASSINFO('D-Bus Introspection', ' <interface name="org.obhub.session">\n' ' <method name="parse">\n' ' <arg direction="in" type="s" name="cmd"/>\n' ' </method>\n' ' </interface>\n') def __init__(self, server, parent): super().__init__(server) self.parent = parent @QtCore.pyqtSlot(str) def parse(self, cmd): passFailed attempt 1, decorator:
#!/usr/bin/python3 from PyQt5 import QtCore, QtDBus class QDBusObject(QtCore.QObject): def __init__(self, parent): QtCore.QObject.__init__(self) self.__dbusAdaptor = QDBusServerAdapter(self, parent) self.start() def start(self): bus = QtDBus.QDBusConnection.sessionBus() bus.registerObject("/org/obhub/session", self) bus.registerService("org.obhub.session") return bus def qDbusHeader(func): def wrapper(*args, **kargs): QtCore.Q_CLASSINFO("D-Bus Interface", "org.obhub.session") QtCore.Q_CLASSINFO('D-Bus Introspection', ' <interface name="org.obhub.session">\n' ' <method name="parse">\n' ' <arg direction="in" type="s" name="cmd"/>\n' ' </method>\n' ' </interface>\n') func(*args, **kargs) return wrapper @qDbusHeader class QDBusServerAdapter(QtDBus.QDBusAbstractAdaptor): def __init__(self, server, parent): super().__init__(server) self.parent = parent @QtCore.pyqtSlot(str) def parse(self, cmd): passFailed attempt 2, metaclass:
#!/usr/bin/python3 from PyQt5 import QtCore, QtDBus class QDBusObject(QtCore.QObject): def __init__(self, parent): QtCore.QObject.__init__(self) self.__dbusAdaptor = MetaClass(self, parent) self.start() def start(self): bus = QtDBus.QDBusConnection.sessionBus() bus.registerObject("/org/obhub/session", self) bus.registerService("org.obhub.session") return bus class QDBusServerAdapter(QtDBus.QDBusAbstractAdaptor): def __init__(self, server, parent): super().__init__(server) self.parent = parent @QtCore.pyqtSlot(str) def parse(self, cmd): pass class MetaClass(QDBusServerAdapter): def __init__(self, server, parent): QtCore.Q_CLASSINFO("D-Bus Interface", "org.obhub.session") QtCore.Q_CLASSINFO('D-Bus Introspection', ' <interface name="org.obhub.session">\n' ' <method name="parse">\n' ' <arg direction="in" type="s" name="cmd"/>\n' ' </method>\n' ' </interface>\n') QDBusServerAdapter.__init__(self, server, parent)