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

Elegant way to disconnect slot after first call(在第一次呼叫后断开插槽的优雅方式)
本文介绍了在第一次呼叫后断开插槽的优雅方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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的指针。

这篇关于在第一次呼叫后断开插槽的优雅方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

error when trying to run an overloaded function with float parameters.(尝试运行带有浮点参数的重载函数时出错。)
C++ lt;lt; operator overloading without friend function(没有友元函数的C++lt;lt;运算符重载)
C++ - How does the compiler decide between overloaded functions with reference types as parameter?(C++-编译器如何决定使用引用类型作为参数的重载函数?)
How can I invoke an overloaded () operator on the keyword this?(如何对关键字this调用重载()运算符?)
How do I denote a pure virtual function in a UML class diagram?(如何在UML类图中表示纯虚函数?)
MPI parallel IO in ASCII format (How do I do it?)(ASCII格式的MPI并行IO(我该怎么做?))