QT:将强类型枚举参数传递到插槽
QT: passing strongly typed enum argument to slot
我定义了一个强类型枚举,如下所示:
enum class RequestType{
type1, type2, type3
};
我还有一个函数定义如下:
sendRequest(RequestType request_type){
// actions here
}
我想每 10 秒调用一次 sendRequest
函数,所以在一个简单的情况下,我会使用这样的东西:
QTimer * timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(sendRequest()));
timer->start(10000);
由于我需要将一些参数传递给sendRequest
函数,我想我必须使用QSignalMapper
但是由于QSignalMapper::setMapping
只能直接用于int
和QString
,因此我无法弄清楚如何实现这一点。有什么相对简单的方法吗?
如果您
使用的是C++ 11,则可以选择调用lambda函数来响应timeout
QTimer * timer = new QTimer(this);
connect(timer, &QTimer::timeout, [=](){
sendRequest(request_type);
});
timer->start(10000);
请注意,这里的连接方法(Qt 5)不使用SIGNAL和SLOT宏,这是有利的,因为错误是在编译时捕获的,而不是在执行期间捕获的。
您可以
创建onTimeout
插槽。像这样:
connect(timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
在此插槽中:
void onTimeout() {
RequestType request;
// fill request
sendRequest(request);
}
相关文章:
- 升压信号2将插槽传递到成员功能以断开连接
- Qt5:连接:如果插槽的参数少于信号,如何使用"连接"
- Qt moveToThread,带有参数的信号/插槽
- 发生 Qt5.11 应用插件构建错误:信号和插槽参数不兼容
- Qt C++:静态断言失败:信号和插槽参数不兼容
- QMetaObject::invokeMethod的插槽参数
- 文档在哪里说明如果参数不是从末端删除参数,则无法从QT信号插槽连接中删除参数?
- 在插槽中传递多个参数
- 传递函数指针代替插槽函数
- 在Qt信号和插槽中使用lambda语法并访问传递的参数
- QTIMER,参数化插槽
- 将多个参数传递给插槽
- Qt 5将带有参数的插槽分配给QPushButton
- 将自定义参数传递到Qt/C++中的插槽
- 在 qt 中传递带有信号和插槽的参数
- Qt:从一个对象发出相同的信号,但参数不同;插槽通过接收的参数进行区分
- QT:将强类型枚举参数传递到插槽
- 将返回对象的信号连接到没有参数的插槽
- 通过QT的信号/插槽系统传递通用异常作为参数
- Qt信号:将动态分配参数的所有权传递给插槽