Qt 匿名信号

发布于 2024-11-01 22:25:42 字数 111 浏览 1 评论 0原文

是否可以在不连接插槽的情况下将信号发送到插槽? 有一个类有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

物价感观 2024-11-08 22:25:42

您可以像调用普通成员函数一样调用对象的(公共)槽。不需要连接。

此外,您不需要提前知道谁将连接到给定的插槽。这种联系可以发生在课堂之外。 (至少对于公共时段而言。)

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.)

零度° 2024-11-08 22:25:42

是的,您可以通过几种方式。

您可以像任何其他 C++ 函数一样调用该槽(如果它是 public)。槽仍然是 C++ 函数。缺点是调用者需要在编译时知道接收者的接口。

logger.log("The frobnitz could not be quuxed");

您可以通过QMetaObject::invokeMethod调用该槽。使用此方法,调用者不需要有关接收者的任何编译时信息,除了它是一个 QObject* 的事实。

if (!QMetaObject::invokeMethod(logger, "log", Q_ARG(QString, "The frobnitz could not be quuxed"))) {
    qWarning("Internal error: logging failed (did someone change the logger API?)");
}

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.

logger.log("The frobnitz could not be quuxed");

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 a QObject*.

if (!QMetaObject::invokeMethod(logger, "log", Q_ARG(QString, "The frobnitz could not be quuxed"))) {
    qWarning("Internal error: logging failed (did someone change the logger API?)");
}
网名女生简单气质 2024-11-08 22:25:42

我认为不存在这种可能性。但也许您可以将 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文