Is there a tidier way to connect many Qt widgets of different types to the same slot?

后端 未结 1 1960
再見小時候
再見小時候 2021-01-21 02:45

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 gr

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-21 03:23

    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 widgets;
    foreach (QWidget * widget, widgets) {
      QMetaProperty prop = widget->metaObject()->userProperty();
      if (!prop.isValid() || !prop.hasNotifySignal())
        continue;
      connect(widget, prop.notifySignal(), this, slot);
    }
    

    0 讨论(0)
提交回复
热议问题