有没有更简洁的方法将许多不同类型的 Qt 小部件连接到同一个插槽? [英] Is there a tidier way to connect many Qt widgets of different types to the same slot?

查看:19
本文介绍了有没有更简洁的方法将许多不同类型的 Qt 小部件连接到同一个插槽?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make an options dialog that saves as much time as possible when applying the settings.

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

The widgets used are spread across 4 tabs and are a selection of group boxes, check boxes, radio buttons, text entry fields, spin counters and combo-boxes amongst possible others but these are most common.

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

I've got a boolean flag in each tab that I want changed to true if any single widget on it changed in some way. this would mean that when the apply method is invoked the dialog can check the each tab's flag to see if the tab has been altered and ignore it if it's unchanged.

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

A sample of my current solution follows, setModified() is the function that sets the flag:

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()));

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

Is there a tidier way to tie all these signals to the same slot? as this is but a fraction of the amount of signals I'm dealing with.

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

My other concern is that this method would fire almost constantly, so i'm also looking for another solution if possible.

推荐答案

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

For editable controls, the value that is edited (checkbox status, list item index, etc.) is called the user property. It is possible to extract the notification signal of such property programmatically, and thus to connect it to a slot:

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 小部件连接到同一个插槽?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆