Skip to content

Instantly share code, notes, and snippets.

@smallevilbeast
Last active December 29, 2015 20:19
Show Gist options
  • Save smallevilbeast/7722919 to your computer and use it in GitHub Desktop.
Save smallevilbeast/7722919 to your computer and use it in GitHub Desktop.
from PyQt5 import QtCore
def with_metaclass(meta, base=QtCore.QObject):
"""Create a base class with a metaclass."""
return meta("NewBase", (base,), {})
class PropertyMeta(QtCore.pyqtWrapperType):
def __new__(cls, cls_name, cls_bases, cls_dict):
super_new = super(PropertyMeta, cls).__new__
props = cls_dict.get("__qtprops__", None)
if props is not None:
for key, values in props.iteritems():
nty = cls_dict['_nty_'+key] = QtCore.pyqtSignal()
_type, default = values
cls_dict["_"+key] = default
def _get(key):
def f(self):
return getattr(self, '_'+key)
return f
def _set(key):
def f(self, value):
setattr(self, '_'+key, value)
getattr(self, "_nty_"+key).emit()
return f
set_func = cls_dict['_set_'+key] = _set(key)
get_func = cls_dict['_get_'+key] = _get(key)
cls_dict[key] = QtCore.pyqtProperty(_type, get_func, set_func, notify=nty)
return super_new(cls, cls_name, cls_bases, cls_dict)
class Job(with_metaclass(PropertyMeta)):
__qtprops__ = {
"name" : (str, "dmusic"),
"groups" : ("QVariant", ["Music", "Dev"]),
"description" : (str, "deepin music player"),
}
class App(QtCore.QObject):
__metaclass__ = PropertyMeta
__qtprops__ = {
"name" : (str, "dmusic"),
"groups" : ("QVariant", ["Music", "Dev"]),
"description" : (str, "deepin music player"),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment