c++ - Qt 静态添加参数到连接指令
问题描述
我的代码如下所示:
connect(c_name, SIGNAL(stateChanged(int) ), employeesList, SLOT(changeVisibility(int)));
c_name
a在哪里QCheckBox
,我想将其连接stateChange
到 中的属性的可见性employeesList
,该方法如下所示:
void changeVisibility(int prop, int visibility){
if(prop & EmployeeListElement::Name)
updateVisibility(&EmployeeListElement::name, visibility);
if(prop & EmployeeListElement::Surname)
updateVisibility(&EmployeeListElement::surname, visibility);
if(prop & EmployeeListElement::DateOfBirth)
updateVisibility(&EmployeeListElement::date_of_birth, visibility);
if(prop & EmployeeListElement::DateOfEmployment)
updateVisibility(&EmployeeListElement::date_of_empl, visibility);
}
private:
void updateVisibility(QLabel* EmployeeListElement::* elem, int visibility){
visibility ? (this->*elem)->show() : (this->*elem)->hide();
}
...
如您所见,我需要传递第二个参数,即我所指的属性,所以我想做这样的事情:
connect(c_name, SIGNAL(stateChanged(int) ), employeesList, SLOT(changeVisibility(int, Class::first_enum_property)));
这不起作用,我的问题是,有没有办法做到这一点?也许没有使用SIGNAL
和SLOT
指令并使用一些(也许)lambdas?
解决方案
使用 lambdas 确实应该可以解决您的问题:
connect(c_name, &QCheckBox::stateChanged, employeesList, [employeesList](int visibility){employeesList->changeVisibility(Class::first_enum_property, visibility);});
请注意,第三个参数(即上下文对象)是可选的,但对于在销毁时自动销毁连接很有用employeesList
。
参考
推荐阅读
- office-js - 如何以编程方式添加和获取 customXMLPart
- javascript - 在这种特殊情况下,$and 和 $all 有什么区别?
- swift - 在 Swift 中使用 WkWebView 获取 HTML
- python - 将json文件拆分成句子
- swift - 无法将 swift 泛型子类转换为泛型超类
- android - 以编程方式将自定义布局添加到 ConstraintLayout
- reactjs - TypeScript 错误:JSX 元素类型“树”没有任何构造或调用签名。我无法导入自己的组件
- swift - 在单元测试中运行异步代码的问题
- android - 无法获取其他用户名
- npm - package.json 中的 phantomChildren 是什么?