是否可以将信号连接到静态插槽而不使用接收器实例? [英] Is it possible to connect a signal to a static slot without a receiver instance?
问题描述
是否可以将信号连接到静态槽而不使用接收器实例?
像这样: connect(& object,SIGNAL ()); STATIC_SLOT(staticFooMember()));
有一个 QApplication :: closeAllWindows / code> function with [static slot] attribute in Qt documentation。有一个使用文档的示例:
exitAct = new QAction(tr(E& xit这个);
exitAct-> setShortcuts(QKeySequence :: Quit);
exitAct-> setStatusTip(tr(退出应用程序));
connect(exitAct,SIGNAL(triggered()),qApp,SLOT(closeAllWindows()));
是否允许执行相同的操作,但不传递实例变量静态函数)?
class Some:public QObject {
Q_OBJECT
public slots:
static void foo();
private:
Some();
};
也许Frank Osterfeld是对的,在这种情况下使用单例模式,但我仍然惊讶为什么此功能尚未实现
更新:
$ b
不允许。相反,允许使用作为静态函数的插槽,但是为了能够连接它,您需要一个实例。
在他们的例子中,
connect(exitAct,SIGNAL ()),qApp,SLOT(closeAllWindows()));
表示比以前调用
QApplication * qApp = QApplication :: instance();
编辑:
连接对象是函数
bool QObject :: connect(const QObject * sender,const QMetaMethod& signal,const QObject * receiver ,const QMetaMethod& method,Qt :: ConnectionType type = Qt :: AutoConnection)
要删除 const QObject * receiver
?
检查 moc
文件,它自己说。
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:
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,
connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));
means than they previously called
QApplication* qApp = QApplication::instance();
Edit:
The only interface for connecting object is the function
bool QObject::connect ( const QObject * sender, const QMetaMethod & signal, const QObject * receiver, const QMetaMethod & method, Qt::ConnectionType type = Qt::AutoConnection )
How are you going to get rid of const QObject * receiver
?
Check the moc
files in your project, it speaks by itself.
这篇关于是否可以将信号连接到静态插槽而不使用接收器实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!