5

我想在QObjects不修改其他信号/插槽行为并且不知道它们的上下文的情况下,一般性地和暂时地阻止两个信号之间的信号。

QObject::blockSignals(bool) 之类的东西,但只在两个之间起作用QObjects

即实现如下SignalBlocker::blockSignals(bool)功能:

class SignalBlocker {

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

private:
  QObject *mSender, *mReceiver;
}

可以通过断开和重新连接对象来实现,但首先必须存储信号/插槽列表。内省方法似乎不足以实现这一目标(我看过QMetaObjectQSignalSpy没有成功)。

4

3 回答 3

11

QT 无法仅禁用信号槽对。试试这个解决方法:

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;
};
于 2013-06-22T12:28:55.763 回答
3

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);
//...
于 2013-03-26T16:32:49.580 回答
0

您可以使用disconnect(sender, 0, receiver, 0);断开所有sender的信号与所有receiver插槽的连接。

于 2013-03-26T10:16:18.460 回答