Is there a tidier way to connect many Qt widgets o

2019-02-24 13:38发布

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

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.

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.

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.

1条回答
淡お忘
2楼-- · 2019-02-24 13:55

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);
}
查看更多
登录 后发表回答