gpt4 book ai didi

qt - 有没有一种更简洁的方法可以将许多不同类型的 Qt 小部件连接到同一个插槽?

转载 作者:行者123 更新时间:2023-12-04 02:45:11 24 4
gpt4 key购买 nike

我正在尝试制作一个选项对话框,以便在应用设置时尽可能节省时间。

使用的小部件分布在 4 个选项卡中,包括一组框、复选框、单选按钮、文本输入字段、旋转计数器和组合框等,但这些是最常见的。

我在每个选项卡中都有一个 bool 标志,如果其中的任何单个小部件以某种方式发生变化,我希望将其更改为 true。这意味着当调用 apply 方法时,对话框可以检查每个选项卡的标志以查看选项卡是否已更改,如果未更改则忽略它。

下面是我当前解决方案的示例,setModified() 是设置标志的函数:

connect(chkShowFormula, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(chkShowAxis, SIGNAL(stateChanged(int)), this, SLOT(setModified()));
connect(cmbAxisType, SIGNAL(currentIndexChanged(int)), this, SLOT(setModified()));
connect(cmbAxisType, SIGNAL(editTextChanged(QString)), this, SLOT(setModified()));
connect(cmbFormat, SIGNAL(currentIndexChanged(int)), this, SLOT(setModified()));
connect(grpShowLabels, SIGNAL(clicked(bool)), this, SLOT(setModified()));
connect(btnAxesFont, SIGNAL(clicked()), this, SLOT(setModified()));
connect(btnLabelFont, SIGNAL(clicked()), this, SLOT(setModified()));

是否有更简洁的方法将所有这些信号绑定(bind)到同一个插槽?因为这只是我正在处理的信号量的一小部分。

我的另一个担心是这个方法几乎会一直触发,所以如果可能的话我也在寻找另一种解决方案。

最佳答案

对于可编辑控件,被编辑的值(复选框状态、列表项索引等)称为用户属性。可以通过编程方式提取此类属性的通知信号,从而将其连接到插槽:

QMetaMethod slot = this->metaObject()->method(this->metaObject()->indexOfSlot("setModified()"));
QList<QWidget*> widgets;
foreach (QWidget * widget, widgets) {
QMetaProperty prop = widget->metaObject()->userProperty();
if (!prop.isValid() || !prop.hasNotifySignal())
continue;
connect(widget, prop.notifySignal(), this, slot);
}

关于qt - 有没有一种更简洁的方法可以将许多不同类型的 Qt 小部件连接到同一个插槽?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18963904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com