使用具有多重继承的Qt信号和插槽 [英] Using Qt signals and slots with multiple inheritance

查看:342
本文介绍了使用具有多重继承的Qt信号和插槽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类(MyClass),它从Qt内置对象(QGraphicsTextItem)继承了大部分功能. QGraphicsTextItemQObject间接继承. MyClass还实现了一个接口MyInterface.

I have a class (MyClass) that inherits most of its functionality from a Qt built-in object (QGraphicsTextItem). QGraphicsTextItem inherits indirectly from QObject. MyClass also implements an interface, MyInterface.

class MyClass : public QGraphicsTextItem, public MyInterface

我需要能够在MyInterface*上使用connectdisconnect.但是看来connectdisconnect仅适用于QObject*实例.由于Qt不支持从QObject派生的类进行多重继承,因此我无法从QObject派生MyInterface. (无论如何,对于界面也没有多大意义.)

I need to be able to use connect and disconnect on MyInterface*. But it appears that connect and disconnect only work on QObject* instances. Since Qt does not support multiple inheritance from QObject-derived classes, I cannot derive MyInterface from QObject. (Nor would that make much sense for an interface anyway.)

在线讨论问题,但是IMO提出了解决方案在通常情况下(通过对象的接口访问对象)是没有用的,因为您不能连接MyInterface*的信号和插槽,而必须将其强制转换为派生类型.由于MyClass是许多MyInterface派生的类之一,因此,如果此语句被广播到该语句,则这将是代码臭味"的,并且违背了该语句的目的.界面.

There is a discussion of the problem online, but IMO the proposed solution is fairly useless in the common case (accessing an object through its interface), because you cannot connect the signals and slots from MyInterface* but must cast it to the derived-type. Since MyClass is one of many MyInterface-derived classes, this would necessitate "code-smelly" if-this-cast-to-this-else-if-that-cast-to-that statements and defeats the purpose of the interface.

此限制是否有好的解决方案?

Is there a good solution to this limitation?

更新:我注意到,如果我dynamic_cast一个MyInterface*QObject*(因为我知道,所有MyInterface派生的类最终也都继承自QObject,它似乎可以正常工作.

UPDATE: I noticed that if I dynamic_cast a MyInterface* to QObject* (because I know all MyInterface-derived classes also inherit eventually from QObject, it seems to work. That is:

MyInterface *my_interface_instance = GetInstance();
connect(dynamic_cast<QObject*>(my_interface_instance), SIGNAL(MyInterfaceSignal()), this, SLOT(TempSlot()));

但是这个真的似乎我在要求未定义的行为....

But this really seems like I am asking for undefined behavior....

推荐答案

您自己找到了答案:dynamic_cast可以按预期工作.这不是未定义的行为.如果您获得的MyInterface实例不是QObject,则强制转换将返回null,并且您可以防止该情况发生(这不会发生,因为您说接口的所有实例也是QObjects).但是请记住,您需要打开RTTI才能使其正常工作.

You found the answer yourself: the dynamic_cast works as you would expect. It is not undefined behavior. If the instance of MyInterface you got is not a QObject, the cast will return null and you can guard yourself against that (which won't happen, since you said all instances of the interface are also QObjects). Remember, however, that you need RTTI turned on for it to work.

我还会提出其他一些建议:

I would also offer a few other suggestions:

  • 使用 Q_INTERFACES 功能(不仅适用于插件).然后,您将根据QObject进行工作,并在真正需要时使用qobject_cast查询MyInterface.我不详细了解您的问题,但是由于您知道所有MyInterface实例也是QObject,因此这似乎是最明智的方法.

  • Use the Q_INTERFACES feature (it's not only for plug-ins). Then you'd work in terms of QObject and query for MyInterface using qobject_cast when it is really needed. I don't know your problem in detail, but since you know that all MyInterface instances are also QObjects, this seems to be the most sensible approach.

向MyInterface添加一个QObject* asQObject()抽象方法,并在所有子类中将其实现为{ return this; }.

Add a QObject* asQObject() abstract method to MyInterface and implement it as { return this; } in all subclasses.

具有 QGraphicsTextItem(组成),而不是成为一个(继承).

Having a QGraphicsTextItem (composition) instead of being one (inheritance).

这篇关于使用具有多重继承的Qt信号和插槽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆