2

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?

erip
  • 16,374
  • 11
  • 66
  • 121
iiirxs
  • 4,493
  • 2
  • 20
  • 35

2 Answers2

3

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.

Community
  • 1
  • 1
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85
2

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);
}
Tony O
  • 513
  • 4
  • 12