[英]QT_INTERFACES and signal definitions
How to define signals in interfaces? 如何在接口中定义信号?
class ISomeInterface
{
signals:
virtual void added(const QString& id) = 0;
virtual void removed(const QString& id) = 0;
// bla, other methods
}
Afaik, signals should not virtual but if I want to define that a signal needs to be implemented in classes implementing the interface => how to do this? Afaik,信号不应该是虚拟的,但是如果我想定义一个信号需要在实现接口的类中实现=>怎么做? Or do you simply make them non-abstract... but interfaces have no Q_OBJECT declaration here!
还是只是简单地使它们抽象化...但是接口在这里没有Q_OBJECT声明! Is the correct code generated in this cases?
在这种情况下是否生成了正确的代码? On top... you'll need a bad (f* * *g) cast to QObject if you want to connect to the signals.
最重要的是,如果要连接到信号,则需要对QObject进行错误的(f * * * g)转换。
class ISomeInterface
{
signals:
void added(const QString& id);
void removed(const QString& id);
// bla, other methods
}
Or do you try to implement it that way? 还是您尝试以这种方式实现它?
class ISomeInterface : public QObject
{
Q_OBJECT
signals:
void added(const QString& id);
void removed(const QString& id);
// bla, other methods
}
Q_DECLARE_INTERFACE(ISomeInterface, "ISomeInterface")
.. but this way I can inherit from only one interface (QObject does not support multiple inheritance). ..但是这种方式只能从一个接口继承(QObject不支持多重继承)。
Conclusion: As suggested, I would go with the first one. 结论:正如建议的那样,我会选择第一个。
You may check this topic for some solutions. 您可以检查此主题以获取一些解决方案。
Unfortunately Qt does not handle multiple inheritance from QObject (see this Q&A ), so you can't derive all your interfaces from it (otherwise you won't be able to derive from multiple interfaces). 不幸的是,Qt不能处理来自QObject的多重继承(请参见本Q&A ),因此您不能从中继承所有接口(否则您将无法从多个接口继承)。 So instead you might have something like
IQObject
interface with 因此,相反,您可能具有类似
IQObject
接口的内容
virtual QObject* qObject() = 0
method, so you don't need to cast them to QObject all the time. 方法,因此您不需要一直将它们强制转换为QObject。
As for providing interface for signals - no such thing. 至于提供信号接口-没有这样的事情。 What you can do is make them virtual abstract, that'll work if you really need to enforce this compile-time check (because
signals:
is just an alias for protected:
). 您可以做的是使它们成为虚拟抽象,如果您确实需要执行此编译时检查(因为
signals:
只是protected:
的别名),则可以使用它们。 If you don't, just check the return value from the connect()
method. 如果不这样做,则只需检查
connect()
方法的返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.