How to pass an additional variable to the Qt slot

I would like to know how to pass a separate variable to a slot. I can't seem to get it to work. Is there any way around this?

This is my code:

QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(method(MYVARIABLE)));
timer->start(4000);
+4
source share
2 answers

If you don't want to declare MYVARIABLEin your class, but are tied to this particular signal / slot connection instead, you can connect the signal to lambda C ++ 11 using Qt5's new single, singal / slot , and then call your slot using lambda.

For example, you can write:

QTimer * timer = new QTimer();
connect(timer, &QTimer::timeout, [=]() {
     method(MYVARIABLE);
});
timer->start(4000);

, ++ 11 Qt5, Qt Property System, QTimer*. QObject::setProperty().

QObject::sender(), QTimer* , QObject::property().

, .

+8

http://doc.qt.io/qt-5/signalsandslots.html

, SIGNAL() SLOT(), , , , SIGNAL(), , SLOT().

QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(methodSlot()));
timer->start(4000);


methodSlot()
{
    method(MYVARIABLE);
}
+2

All Articles