在第一次呼叫后断开插槽的优雅方式

2022-03-07 00:00:00 signals qt c++ slot

在构造函数内部,有一个连接:

connect(&amskspace::on_board_computer_model::self(),
      SIGNAL(camera_status_changed(const amskspace::camera_status_t&)),
      this,
      SLOT(set_camera_status(const amskspace::camera_status_t&)));

和方法:

void camera_model::
set_camera_status(const amskspace::camera_status_t& status) {
  disconnect(&amskspace::on_board_computer_model::self(),
             SIGNAL(camera_status_changed(const amskspace::camera_status_t&)),
             this,
             SLOT(set_camera_status(const amskspace::camera_status_t&)));

  // do the job
}

并且我想在第一次呼叫后断开此插槽的连接。

问题是:有没有办法只调用一次插槽?而不会显式地断开连接?比如单枪法?有可能吗?


解决方案

核心思想是创建包装器,即自动断开信号连接的特殊"连接"。如果您使用大量的"call me once"连接,这很有用;否则我建议您在插槽开始时使用QObject::Disconnect。

此实现通过创建两个连接来工作:一个是"正常"连接,另一个是立即断开连接&;清除所有连接。

实现(使用C++11/Qt 5,):

template <typename Func1, typename Func2>
static inline QMetaObject::Connection weakConnect(
        typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
        typename QtPrivate::FunctionPointer<Func2>::Object *receiver, Func2 slot)
{

    QMetaObject::Connection conn_normal = QObject::connect(sender, signal, receiver, slot);

    QMetaObject::Connection* conn_delete = new QMetaObject::Connection();

    *conn_delete = QObject::connect(sender, signal, [conn_normal, conn_delete](){
        QObject::disconnect(conn_normal);
        QObject::disconnect(*conn_delete);
        delete conn_delete;
    });
    return conn_normal;
}

注意事项/需要改进的地方:

  • 清理发生在调用常规槽之后。如果规则槽导致再次发出信号,则将再次执行规则槽(可能会导致无限递归)。
  • 除了发出信号外,没有正确的断开方式。(您可以使用QObject::disconnect,但这会导致较小的内存泄漏)
  • 取决于槽的执行顺序。现在看起来还可以。
  • 命名

测试使用:

class A : public QObject
{
    Q_OBJECT
signals:
    void sig(int a);
};


class B : public QObject
{
    Q_OBJECT
public:
    B(int b) : QObject(), b_(b) {}
    int b() const { return b_; }
public slots:
    void slo(int a) { qDebug() << "	B :" << b_ << "a:" << a;  }
private:
    int b_;
};

A a1;
A a2;

B b10(10);
B b20(20);

weakConnect(&a1, &A::sig, &b10, &B::slo);
weakConnect(&a1, &A::sig, &b20, &B::slo);
weakConnect(&a2, &A::sig, &b20, &B::slo);

qDebug() << "a1 :"; emit a1.sig(1);// Should trigger b10 and b20 slo
qDebug() << "a2 :"; emit a2.sig(2);// Should trigger b20 slo
qDebug() << "a1 :"; emit a1.sig(3);// Should do nothing
qDebug() << "a2 :"; emit a2.sig(4);// Should do nothing

测试代码输出:

a1 :
    B : 10 a: 1
    B : 20 a: 1
a2 :
    B : 20 a: 2
a1 :
a2 :

去掉C++11/Qt5(我没有Qt 4.8/GCC4.4.7,所以没有测试过) 根据文档,Qt4.8没有连接到功能,所以我使用的是包装器:

class ConnectJanitor : public QObject
{
    Q_OBJECT
public slots:
    void cleanup()
    {
        QObject::disconnect(conn_normal_);
        QObject::disconnect(*conn_delete_);
        delete conn_delete_;
        delete this;
    }
public:
    static ConnectJanitor* make(QMetaObject::Connection conn_normal,
                                QMetaObject::Connection* conn_delete)
    {
        return new ConnectJanitor(conn_normal, conn_delete);
    }
private:
    ConnectJanitor(QMetaObject::Connection conn_normal,
                   QMetaObject::Connection* conn_delete) :
        QObject(0) , conn_normal_(conn_normal), conn_delete_(conn_delete) {}

    ConnectJanitor(const ConnectJanitor&); // not implemented
    ConnectJanitor& operator=(ConnectJanitor const&);


    QMetaObject::Connection conn_normal_;
    QMetaObject::Connection* conn_delete_;
};

(我将ConnectJanitor的构造函数设为私有,因为实例会自毁(delete this))

weakConnect

static inline QMetaObject::Connection weakConnect(const QObject * sender, const char * signal, const QObject * receiver, const char * slot)
{
    QMetaObject::Connection conn_normal = QObject::connect(sender, signal, receiver, slot);
    QMetaObject::Connection* conn_delete = new QMetaObject::Connection();
    *conn_delete = QObject::connect(sender, signal, ConnectJanitor::make(conn_normal, conn_delete), SLOT(cleanup()));
    return conn_normal;
}

如果您需要手动断开连接,我建议您让Weinder Connect()返回ConnectJanitor的指针。

相关文章