是否可以在没有接收器实例的情况下将信号连接到静态插槽?
是否可以在没有接收器实例的情况下将信号连接到静态插槽?
像这样: connect(&object, SIGNAL(some()), STATIC_SLOT(staticFooMember()));
有一个带有 [ 的 QApplication::closeAllWindows()
函数Qt 文档中的 static slot] 属性。文档中有一个使用它的示例:
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
是否允许执行相同的操作但不传递实例变量(例如,当类仅具有静态函数时)?
class Some : public QObject {
Q_OBJECT
public slots:
static void foo();
private:
Some();
};
也许 Frank Osterfeld 是对的,在这种情况下最好使用单例模式,但我仍然很惊讶为什么这个功能还没有实现。
更新:
Is it possible to connect a signal to static slot without receiver instance?
Like this: connect(&object, SIGNAL(some()), STATIC_SLOT(staticFooMember()));
There is a QApplication::closeAllWindows()
function with [static slot] attribute in Qt documentation. And there is an example of using it from the documentation:
exitAct = new QAction(tr("E&xit"), this);
exitAct->setShortcuts(QKeySequence::Quit);
exitAct->setStatusTip(tr("Exit the application"));
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
Is it allowed to do the same action but without passing an instance variable (e.g. when a class has only static functions)?
class Some : public QObject {
Q_OBJECT
public slots:
static void foo();
private:
Some();
};
Maybe Frank Osterfeld is right and it is better to use singleton pattern in this case but I am still surprised why this feature has not been implemented yet.
Update:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
QT5 更新:是的,您可以
在 QT4 中您不能:
不,这是不允许的。相反,允许使用静态函数插槽,但为了能够连接它,您需要一个实例。
在他们的示例中,
意味着比他们之前调用的
编辑:
连接对象的唯一接口是函数
如何摆脱
const QObject *receiver
?检查项目中的
moc
文件,它会自己说话。Update for QT5: Yes you can
In QT4 you can't:
No it is not allowed. Rather, it is allowed to use a slot which is a static function, but to be able to connect it you need an instance.
In their example,
means than they previously called
Edit:
The only interface for connecting object is the function
How are you going to get rid of
const QObject * receiver
?Check the
moc
files in your project, it speaks by itself.是的。(使用 Qt5)
It is. (With Qt5)
在 Qt 的早期版本中,虽然您不能像 @UmNyobe 提到的那样这样做,但如果您确实想调用该静态槽,则可以执行以下操作:
In earlier versions of Qt, although you cannot do so as mentioned by @UmNyobe but you can do something like this if you really want to call that static slot :