Temporarily block signals between two QObjects

2019-05-03 07:39发布

I would like to generically and temporarily block the signals between two QObjects without modifying the other signals/slots behavior, and without knowing their contexts.

Something like QObject::blockSignals(bool), but only acting between two QObjects.

That is, implementing the following SignalBlocker::blockSignals(bool) function:

class SignalBlocker {

public:
  SignalBlocker(QObject *sender, QObject *receiver) :
    mSender(sender), mReceiver(receiver) {}
  void blockSignals(bool block);

private:
  QObject *mSender, *mReceiver;
}

It would be possible by disconneting and re-connecting the objects, but first the list of signals/slots would have to be stored. Introspection methods don't seem to be powerful enough to achieve this (I looked at QMetaObject and QSignalSpy without success).

3条回答
三岁会撩人
2楼-- · 2019-05-03 08:15

U can use disconnect(sender, 0, receiver, 0); to disconnect all sender's signals from all receiver's slots.

查看更多
放我归山
3楼-- · 2019-05-03 08:37

since you want that the sender and the reciever will not send signals within that scope, i would just try to use blockSignals(bool)

class SignalBlocker{
public:
SignalBlocker(QObject* obj)
{
   _o = obj;
   _o->blockSignals(true);
}
~SignalBlocker()
{
   _o->blockSignals(false);
}
private:
   QObject* _o;
};

and now just use

SignalBlocker sb1(sender);
SignalBlocker sb2(reciever);
//...
查看更多
我只想做你的唯一
4楼-- · 2019-05-03 08:38

QT have no capabilities to disable signal-slot pair only. Try this workaround:

struct SignalDisabler
{
    SignalDisabler(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
        : sender(sender)
        , signal(signal)
        , receiver(receiver)
        , member(member)
    {
        QObject::disconnect(sender, signal, receiver, member);
    }

    ~SignalDisabler()
    {
        QObject::connect(sender, signal, receiver, member);
    }

    const QObject *sender;
    const char *signal;
    const QObject *receiver;
    const char *member;
};
查看更多
登录 后发表回答