I have defined a strongly typed enum like this:
enum class RequestType{
type1, type2, type3
};
Also I have a function defined as below:
sendRequest(RequestType request_type){
// actions here
}
I'd like to call the sendRequest
function every 10 seconds so in a simple case I would use something like this:
QTimer * timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest()));
timer->start(10000);
Since I need to pass some arguments to sendRequest
function I guess I have to use QSignalMapper
but as QSignalMapper::setMapping
can be used straightforwardly only for int
and QString
, I cannot figure out how to implement this. Is there any relatively simple way for it?
If you're using C++ 11 , you have the option of calling a lambda function in response to timeout
QTimer * timer = new QTimer(this);
connect(timer, &QTimer::timeout, [=](){
sendRequest(request_type);
});
timer->start(10000);
Note that the connection method here (Qt 5) doesn't use the SIGNAL and SLOT macros, which is advantageous, as errors are caught at compilation, rather than during execution.
You can create onTimeout
slot. Something like this:
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
and in this slot:
void onTimeout() {
RequestType request;
// fill request
sendRequest(request);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.