如何识别插槽中发出的信号?

时间:2018-06-03 11:38:53

标签: c++ qt qt-signals slot

我将两个信号连接到同一个插槽。像这样:

check = new QCheckBox();
connect(check, SIGNAL(clicked()), this, SLOT(MySlot()));
connect(check, SIGNAL(toggled(bool)),this,SLOT(MySlot()));

我不想定义其他插槽。在MySlot中是否可以识别哪个信号回调插槽? 我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:3)

您可以使用与发件人相关联的QMetaObject / QMetaMethod数据来获取您想要的内容(未经测试)......

void MyClass::MySlot ()
{
  auto index = senderSignalIndex();
  if (index == sender()->indexOfSignal("clicked()")) {

    /*
     * Got here as the result of a clicked() signal.
     */
  } else if (index == sender()->indexOfSignal("toggled(bool)")) {

    /*
     * Got here as the result of a toggled(bool) signal.
     */
  }
}

然而,如果您使用Qt5,我会建议使用新的信号/插槽语法以及lambdas ......

check = new QCheckBox();
connect(check, &QCheckBox::clicked,
        [this]()
        {
          MySlot(false);
        });
connect(check, &QCheckBox::toggled,
        [this](bool toggled)
        {
          MySlot(true, toggled);
        });

随着对MySlot的签名的更改......

/**
 * @param from_toggled_signal If true this call was triggered by a
 *                            QCheckBox::toggled signal, otherwise it's
 *                            the result of a QCheckBox::clicked signal.
 *
 * @param toggle_value If from_toggled_signal is true then this was the
 *                     value passed to QCheckBox::toggled, otherwise unused.
 */
void MyClass::MySlot (bool from_toggled_signal, bool toggle_value = false)
{
  .
  .
  .
}

答案 1 :(得分:2)

New slots can be defined on the fly using lambdas :)

class MyClass : public QWidget {
  QSomeLayout m_layout{this};
  QCheckBox m_check;
  enum Signal { Clicked, Toggled };

  Q_SLOT void mySlot(Signal);
public:
  MyClass( ... ) : ... {
    m_layout.addWidget(&m_check);
    connect(&m_check, &QCheckBox::clicked, this, [this]{ mySlot(Clicked); });
    connect(&m_check, &QCheckBox::toggled, this, [this]{ mySlot(Toggled); });
  }
};

答案 2 :(得分:0)

如果有帮助,您还可以将自己的上下文添加到信号中。例如,我有一个服务,下载多个窗口的用户头像。我需要窗口只加载它感兴趣的用户,所以我会传入用户的id作为上下文。类似的东西:

void UserService::downloadAvatar(const QString& url, const int context = 0) {
  ...// Make the http request, on finished:

  emit onAvatarDownloaded(context, responseBody);

}