2016-03-01 96 views
2

我已經定義了一個強類型枚舉這樣的:QT:使強類型枚舉參數時隙

enum class RequestType{ 
    type1, type2, type3 
}; 

還我一個函數定義如下:

sendRequest(RequestType request_type){ 
    // actions here 
} 

我想調用sendRequest功能每隔10秒鐘,這樣在簡單的情況下,我會使用這樣的:

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

由於我需要通過一些參數sendRequest函數我想我必須使用QSignalMapperQSignalMapper::setMapping可以直接用於intQString,我不知道如何實現這一點。有沒有比較簡單的方法呢?

+2

請記住,強類型枚舉需要註冊爲元類型。 – user3528438

回答

2

如果你using C++ 11,你必須調用響應lambda函數來timeout

QTimer * timer = new QTimer(this); 
connect(timer, &QTimer::timeout, [=](){ 

    sendRequest(request_type); 

}); 
timer->start(10000); 

注意選項這裏的連接方法(Qt 5)不使用SIGNAL和SLOT宏,這是有利的,因爲在編譯時捕獲錯誤,而不是在執行期間捕獲錯誤。

2

您可以創建onTimeout插槽。事情是這樣的:

connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout())); 

,並在此插槽:

void onTimeout() { 
    RequestType request; 
    // fill request 
    sendRequest(request); 
}