如何/应该在Qt中创建ui表单和数据成员之间的自动链接
How can/should I create automatic link between ui forms and data members in Qt?
这是一个通用问题,我不知道通常是如何处理的,我正在寻找一种好的(??)方法:
我有一个ui,它有许多数字形式,对应于我程序的各种选项。我有几个使用ui提供的数据的结构。
显然,我需要同步表单和数据。现在我手动完成,并编写这样的函数:
Options GetOptions(){ //fetches data from ui and stores it in my structure
options.fil.alpha = ui.fil_consecutive_alpha->value();
options.fil.beta = ui.fil_consecutive_beta->value();
options.fil.gamma = ui.fil_consecutive_gamma->value();
options.fil.delta = ui.fil_consecutive_delta->value();
options.fil.k_max = ui.fil_consecutive_k_max->value();
options.fil.radius = ui.fil_consecutive_radius->value();
options.fil.omega = ui.fil_consecutive_omega->value();
options.fil.side_length = ui.fil_consecutive_side_length->value();
options.fil.norm = ui.fil_consecutive_norm->value();
options.fil.consecutive_images = true;
}
void SetOptions(const Options& options){ //update ui forms with the loaded options sdtored in the structure
ui.fil_consecutive_alpha->setValue(options.fil.alpha);
ui.fil_consecutive_beta->setValue(options.fil.beta );
ui.fil_consecutive_gamma->setValue(options.fil.gamma);
ui.fil_consecutive_delta->setValue(options.fil.delta);
ui.fil_consecutive_k_max->setValue(options.fil.k_max);
ui.fil_consecutive_radius->setValue(options.fil.radius);
ui.fil_consecutive_omega->setValue(options.fil.omega);
ui.fil_consecutive_side_length->setValue(options.fil.side_length);
ui.fil_consecutive_norm->setValue(options.fil.norm);
}
每次添加ui字段时,我都必须更新集合并获取函数。这些功能看起来很笨,而且随着时间的推移,情况越来越糟,我被告知计算机擅长处理这种重复的笨任务。
那么,你认为以某种方式说每个以"fil_consective_"开头的ui表单都应该与结构选项.fil相关联是个好主意吗?我如何通过程序实现这一点?
我会给你一个想法。您可以将objectName设置为窗体。如果在Designer中设置ObjectName,则将在生成的ui.hh文件中完成。使用QList存储所有特定表单和选项的QMap。
...
// set form's name somewhere
ui.fil_consecutive_alpha->setObjectName("fil_consecutive_omega");
...
...
// colect forms like this
QObjectList list = this.children();
QObjectList forms = QObjectList();
for (int i = 0; i < list.size(); ++i) {
if list[i].objectName().startsWith("fil_consecutive_")
forms << list[i];
...
...
// then your function will be
void getOptions(QObjectList forms){
...
for (int i = 0; i < forms.size(); ++i) {
QString opt = forms[i].objectName().mid(QString("fil_consecurive_").length())
options.fil[opt] = forms[i].property("value");
}
...
}
相关文章:
- 用于访问容器<T>数据成员的正确 API
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 数据成员SFINAE的C++17测试:gcc vs clang
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 静态数据成员模板专用化的实例化点在哪里
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 使用指针访问数组中的对象数据成员
- 友元函数无法访问私有数据成员 (c++)
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- 将私有数据成员添加到野牛生成的类中
- 输入数据成员未按要求工作
- 二维矢量数据成员
- C++中类数据成员之间的通信
- 对于数据成员,如果包含对象已在动态内存中,则动态分配此变量(或不动态分配)之间是否有任何区别
- 基础和派生类C 之间的继承数据成员
- 非静态数据成员上的成员初始值设定项列表和默认成员初始值设定项之间有什么区别?
- 如何/应该在Qt中创建ui表单和数据成员之间的自动链接
- c++中使用相同数据成员和函数的两个类之间的继承