从 Qt 中的静态类方法发送信号
我正在尝试编写一个静态回调函数,该函数经常从同一类中的另一个静态函数调用。我的回调函数需要发出
一个信号,但由于某种原因它根本无法这样做。我已将其放在调试器下,并且 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是行不通的,因为每次输入该静态函数时都会创建一个新的 Foo,并且不会将信号连接到插槽。
因此,解决方法是将对象传递给该函数:
另一个选择是使用 postEvent,但我不推荐它。
由于您无法修改回调的签名,因此您可以这样做:
但是您必须在某处初始化该静态变量。
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 :
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 :
but you'll have to initialize that static variable somewhere.
如果有人仍然找到解决方案,这就是我所做的,它适用于我的项目。
1.让你的班级成为单例
2.在静态cb函数中,从您的单例类加载emitFunction
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
有一个优雅的解决方案。您可以在静态成员函数中发出信号,如下所示:
There is an elegant solution. You can emit a signal in a static member function like this:
您必须提供指向类实例的指针才能使其工作。
但请注意,通常不建议从静态函数发出信号。可以在不创建类实例的情况下调用静态函数,这意味着(如果您不提供发送者作为参数并明确使用它),您将不会有一个用于发出信号的“发送者”对象。
对于 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.