我有一个巨大的特征应用程序,它正在遇到恩惠特征的局限性。使用@on_traits_changed装饰器时主要是性能问题。如果可以的话,用PyQt4(或PyQt5)信号来解决这些问题是非常简单的:
from traits.api import *
from PyQt4 import QtCore
class Foo(HasTraits, QtCore.QObject):
pass
错误堆栈:
TypeError Traceback (most recent call last)
<ipython-input-3-ecdfa57492f7> in <module>()
2 from PyQt4 import QtCore
3
----> 4 class Foo(HasTraits, QtCore.QObject):
5 pass
C:\Python27\lib\site-packages\traits\has_traits.pyc in __new__(cls, class_name,
bases, class_dict)
427
428 # Finish building the class using the updated class dictionary:
--> 429 klass = type.__new__( cls, class_name, bases, class_dict )
430
431 # Fix up all self referential traits to refer to this class:
TypeError: Error when calling the metaclass bases
metaclass conflict: the metaclass of a derived class must be a (non-strict)
subclass of the metaclasses of all its bases
但据我所知,这是不可能的。有一些解决方法吗?
编辑:添加了进口
EDIT2:添加了错误堆栈
答案 0 :(得分:1)
我建议的解决方案是忘记Qt。您可以使用纯Python进行自定义Qt信号和插槽的任何操作。下面是一个类PSignal的示例,它与Qt中的Signal具有完全相同的公共接口。 PSignal的实例可以添加到任何Python对象,而无需子类化QObject,甚至根本不使用Qt库。与Qt Signals不同,它们可以在任何类的__init__
方法中实例化为实例变量,而不是在类级别(如Qt所要求的)。此外,emit
是一种标准的Python方法,可以使用任意数量的参数和关键字,从而使整个机制更加“Pythonic”。这里唯一缺少的是信号和插槽的线程切换行为,这似乎不是必需的。
通过覆盖子类中的方法emit
和_emit
,您可以将这个简单的小工具适应各种情况,这是使用Qt信号无法轻松完成的。
DEBUG = False
class PSignal:
def __init__(self):
self.__handlers = []
def connect(self,f):
"""f is a python function."""
if not callable(f):
raise ValueError("Object {!r} is not callable".format(f))
self.__handlers.append(f)
if DEBUG:
print("Connecting",f,self.__handlers)
def disconnect(self,f):
for f1 in self.__handlers:
if f == f1:
self.__handlers.remove(f)
return
def emit(self,*x,**y):
self._emit(*x,**y)
def _emit(self,*x,**y):
for f in self.__handlers:
try:
if DEBUG:
print("emit",f,len(x),x,y)
f(*x,**y)
except Exception:
print("Error in signal",f)
traceback.print_exc()