[英]Qt connect() without QObject or slots
I know what I am trying to achieve is possible, because I can do it with a lambda expression and I have done it before (Few months ago I just don't remember the syntax). 我知道我想要实现的是可能的,因为我可以用lambda表达式来完成它,之前我已经完成了它(几个月前我只是不记得语法)。 Basically, I want to connect a function to a timer/button/etc.
基本上,我想将一个函数连接到一个计时器/按钮/等。 to facilitate the workings of an event.
促进活动的运作。
Here is my working code: 这是我的工作代码:
connect( &renderTimer, &QTimer::timeout, [ = ]() {
onTimerUpdate();
} );
That uses a lambda to connect to the slot. 这使用lambda连接到插槽。 I want to just reference the function itself instead of using a lambda expression.
我想只引用函数本身而不是使用lambda表达式。 I have tried inserting the method
onTimerUpdate()
and &onTimerUpdate
none of which work. 我试过插入方法
onTimerUpdate()
和&onTimerUpdate
都没有工作。 I don't want to use QObject
or any of it's pre-generated bullcrap — nor do I want to define slots within my class. 我不想使用
QObject
或其中任何预先生成的bullcrap - 我也不想在我的班级中定义插槽。 I want to, quite simply, connect it directly to my function. 我想简单地将它直接连接到我的函数。
This is the format when connecting to a member function: 这是连接到成员函数时的格式:
QObject::connect(&renderTimer, &QTimer::timeout, this, &ArclightGLWidget::onTimerUpdate);
This is the format when connecting to a free funciton (same for lambda) 这是连接到自由函数时的格式(对于lambda也是如此)
QObject::connect(&renderTimer, &QTimer::timeout, onTimerUpdate);
And this is the format when connecting to a static member function: 这是连接到静态成员函数时的格式:
QObject::connect(&renderTimer, &QTimer::timeout, SomeType::onTimerUpdate);
Presumably this call is being made inside the class of which onTimerUpdate
is a method. 据推测,这个调用是在
onTimerUpdate
是一个方法的类中进行的。 If so you can call: 如果是这样,你可以致电:
connect(&renderTimer, &QTimer::timeout, this, &foo::onTimerUpdate);
This is preferable to the lambda because Qt will manage the connection and you will also be able to call sender()
in the slot
. 这比lambda更好,因为Qt将管理连接,你也可以在
slot
调用sender()
。
However if you are within the class in which onTimerUpdate
is a method you can also use the lambda exactly as you have put in the question: 但是,如果您在
onTimerUpdate
是一个方法的类中,您也可以完全按照您的问题使用lambda:
connect( &renderTimer, &QTimer::timeout, [=](){onTimerUpdate();} );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.