Qt 匿名信号
是否可以在不连接插槽的情况下将信号发送到插槽? 有一个类有一个 SLOT,可以显示一些日志。 目前,我们不知道将使用多少个类向此日志槽发送信号,并且我们无法将它们的对象相互寻址,但每个对象都可能发送日志请求。
Is it possible to send signals to a slot without connecting them?
There is a class that has a SLOT which shows some logs.
For now we don't have any information how many classes will be use to send signals to this log slot, and we won't be able to address their objects to each other, but every objects might send logging request.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以像调用普通成员函数一样调用对象的(公共)槽。不需要连接。
此外,您不需要提前知道谁将连接到给定的插槽。这种联系可以发生在课堂之外。 (至少对于公共时段而言。)
You can call an object's (public) slot just like you call a normal member function. A connection is not necessary.
Besides, you don't need to know in advance who will connect to a given slot. The connection can happen outside your class. (For public slots at least.)
是的,您可以通过几种方式。
您可以像任何其他 C++ 函数一样调用该槽(如果它是
public
)。槽仍然是 C++ 函数。缺点是调用者需要在编译时知道接收者的接口。您可以通过
QMetaObject::invokeMethod
调用该槽。使用此方法,调用者不需要有关接收者的任何编译时信息,除了它是一个QObject*
的事实。Yes you may, in a few ways.
You may call the slot like any other C++ function (if it is
public
). Slots are still C++ functions. The downside is that the caller needs to know the receiver's interface at compile time.You may invoke the slot via
QMetaObject::invokeMethod
. With this method, the caller doesn't need any compile-time info about the recipient other than the fact that it is aQObject*
.我认为不存在这种可能性。但也许您可以将 log() 方法设为静态,这样您就可以在不引用记录器对象的情况下调用 log() 方法?
I think there is no such possibility. But maybe you can just make log() method static, so you will be able to call log() method without referencing logger object?