是否可以在没有接收器实例的情况下将信号连接到静态插槽?

发布于 2025-01-08 07:56:27 字数 866 浏览 5 评论 0原文

是否可以在没有接收器实例的情况下将信号连接到静态插槽?

像这样: 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 是对的,在这种情况下最好使用单例模式,但我仍然很惊讶为什么这个功能还没有实现。

更新:

在 Qt 5 中这是可能的

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:

In Qt 5 it is possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

我喜欢麦丽素 2025-01-15 07:56:27

QT5 更新:是的,您可以

static void someFunction() {
    qDebug() << "pressed";
}
// ... somewhere else
QObject::connect(button, &QPushButton::clicked, someFunction);

在 QT4 中您不能:

不,这是不允许的。相反,允许使用静态函数插槽,但为了能够连接它,您需要一个实例。

在他们的示例中,

connect(exitAct, SIGNAL(triggered()), 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 文件,它会自己说话。

Update for QT5: Yes you can

static void someFunction() {
    qDebug() << "pressed";
}
// ... somewhere else
QObject::connect(button, &QPushButton::clicked, someFunction);

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,

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.

儭儭莪哋寶赑 2025-01-15 07:56:27

是的。(使用 Qt5)

#include <QApplication>
#include <QDebug>

void foo(){
    qDebug() << "focusChanged";
}


int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QObject::connect(&app, &QApplication::focusChanged, foo);
    return app.exec();
}

It is. (With Qt5)

#include <QApplication>
#include <QDebug>

void foo(){
    qDebug() << "focusChanged";
}


int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    QObject::connect(&app, &QApplication::focusChanged, foo);
    return app.exec();
}
寄居人 2025-01-15 07:56:27

在 Qt 的早期版本中,虽然您不能像 @UmNyobe 提到的那样这样做,但如果您确实想调用该静态槽,则可以执行以下操作:

connect(&object, SIGNAL(some()), this, SLOT(foo()));

void foo()
{
    .... //call your static function here.
}

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 :

connect(&object, SIGNAL(some()), this, SLOT(foo()));

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