I would like to know how to pass a separate variable into a slot. I cant seem to get it to work. Is there some way around this?
This is my code:
QTimer * timer = new QTimer();
connect(timer,SIGNAL(timeout()),this,SLOT(method(MYVARIABLE)));
timer->start(4000);
If you don't want to declare MYVARIABLE
in your class, but instead to have it tied to this particular signal/slot connection, you can connect the signal to a C++11 lambda using Qt5's new singal/slot syntax and then call your slot with that lambda.
For example you could write:
QTimer * timer = new QTimer();
connect(timer, &QTimer::timeout, [=]() {
method(MYVARIABLE);
});
timer->start(4000);
Another solution if you can't use C++11 and Qt5 is to use Qt's Property System to attach a variable to your QTimer*
. This can be done with QObject::setProperty()
.
Then in the slot you could use QObject::sender()
to get your QTimer*
and read the property back using QObject::property()
.
However, note that it's not a very clean solution, and borderline abuse of the property system.