从 Qt 中的静态类方法发送信号

发布于 2025-01-08 08:14:39 字数 670 浏览 6 评论 0原文

我正在尝试编写一个静态回调函数,该函数经常从同一类中的另一个静态函数调用。我的回调函数需要发出一个信号,但由于某种原因它根本无法这样做。我已将其放在调试器下,并且 slot 永远不会被调用。但是,当我将用于发出数据的代码放置在非静态函数中时,它就可以工作了。我无法从静态函数发出信号是否有原因?我尝试声明该类的一个新实例并调用emit 函数,但没有成功。

class Foo
{
signals:
    emitFunction(int);
private:
    static int callback(int val)
    {
        /* Called multiple times (100+) */
        Foo *foo = new Foo;
        foo.emitFunction(val);
    }
    void run()
    {
        callback(percentdownloaded);
    }
};

我已经发布了一些基本代码来演示我正在尝试做的事情。我将根据要求发布完整的代码。

编辑:我发布了完整的代码,因为这是一种奇怪的情况。 http://pastebin.com/6J2D2hnM

I am trying to code a static callback function that is called frequently from another static function within the same class. My callback function needs to emit a signal but for some reason it simply fails to do so. I have put it under a debugger and the slot never gets called. However when I place the code I used to emit the data in a non-static function it works. Is there a reason I cannot emit a signal from a static function? I have tried declaring a new instance of the class and calling the emit function but with no luck.

class Foo
{
signals:
    emitFunction(int);
private:
    static int callback(int val)
    {
        /* Called multiple times (100+) */
        Foo *foo = new Foo;
        foo.emitFunction(val);
    }
    void run()
    {
        callback(percentdownloaded);
    }
};

I have posted some basic code that demonstrates what I am attempting to do. I will post full code upon request.

Edit: I am posting the full code since this is kind of an odd scenario. http://pastebin.com/6J2D2hnM

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

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

发布评论

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

评论(4

jJeQQOZ5 2025-01-15 08:14:39

这是行不通的,因为每次输入该静态函数时都会创建一个新的 Foo,并且不会将信号连接到插槽。

因此,解决方法是将对象传递给该函数:

class Foo
{
signals:
    emitFunction(int);
private:
    static int callback(int val, Foo &foo)
    {
        /* Called multiple times (100+) */
        foo.emitFunction(val);
    }
    void run()
    {
        callback(percentdownloaded, *this);
    }
};

另一个选择是使用 postEvent,但我不推荐它。


由于您无法修改回调的签名,因此您可以这样做:

class Foo
{
signals:
    emitFunction(int);
private:
    static int callback(int val)
    {
        /* Called multiple times (100+) */
        theFoo->emitFunction(val);
    }
    static Foo *theFoo;
    void run()
    {
        callback(percentdownloaded, *this);
    }
};

但是您必须在某处初始化该静态变量。

That is not going to work, because you are creating a new Foo every time you enter that static function, and you do not connect a signal to a slot.

So, the fix would be to pass the object to that function :

class Foo
{
signals:
    emitFunction(int);
private:
    static int callback(int val, Foo &foo)
    {
        /* Called multiple times (100+) */
        foo.emitFunction(val);
    }
    void run()
    {
        callback(percentdownloaded, *this);
    }
};

Another option is to use postEvent, but I wouldn't recommend it.


Since you can not modify callback's signature, you can do it like this :

class Foo
{
signals:
    emitFunction(int);
private:
    static int callback(int val)
    {
        /* Called multiple times (100+) */
        theFoo->emitFunction(val);
    }
    static Foo *theFoo;
    void run()
    {
        callback(percentdownloaded, *this);
    }
};

but you'll have to initialize that static variable somewhere.

但可醉心 2025-01-15 08:14:39

如果有人仍然找到解决方案,这就是我所做的,它适用于我的项目。
1.让你的班级成为单例
2.在静态cb函数中,从您的单例类加载emitFunction

    static int callback(int val)
   {
   /* Called multiple times (100+) */
   MYClass::getInstance()->emitFunction(val);
   }

in case someone still finding the solution, here is what I did, it works in my project.
1. make your class to be a singleton
2. in static cb function , load emitFunction from the your singleton class

    static int callback(int val)
   {
   /* Called multiple times (100+) */
   MYClass::getInstance()->emitFunction(val);
   }
冰雪之触 2025-01-15 08:14:39

有一个优雅的解决方案。您可以在静态成员函数中发出信号,如下所示:

emit (instance().newMessage(message));

There is an elegant solution. You can emit a signal in a static member function like this:

emit (instance().newMessage(message));
马蹄踏│碎落叶 2025-01-15 08:14:39

您必须提供指向类实例的指针才能使其工作。

但请注意,通常不建议从静态函数发出信号。可以在不创建类实例的情况下调用静态函数,这意味着(如果您不提供发送者作为参数并明确使用它),您将不会有一个用于发出信号的“发送者”对象。

对于 Lol4t0 所建议的这些情况,我也更喜欢回调。

You must provide a pointer to the class instance in order to make it work.

Notice however that in general it is not advised to emit signals from static functions. Static functions can be called without creating an instance of the class, which means that (if you do not provide as argument the sender and use it explicitely), you will not have a "sender" object for the emited signals.

For these cases as Lol4t0 suggests I also prefer callbacks.

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