Qt里通过传递函数指针实现动态绑定信号/槽 (**)

目录


Qt中信号槽形参值传递,引用传递,指针传递的实例及总结

https://blog.youkuaiyun.com/u011555996/article/details/124436777

信号与槽QVariant传递结构体指针

  https://blog.youkuaiyun.com/qq_42985268/article/details/115674979

Qt中信号形参值传递,引用传递,指针传递的不同

  https://blog.youkuaiyun.com/qq_45662588/article/details/119888994

----------------------------------------------------

参考:

QVariant 存取任意类型的指针 、自定义数据类型
https://blog.youkuaiyun.com/ken2232/article/details/131733264

QVariant类
https://blog.youkuaiyun.com/ken2232/article/details/131732801

Qt 子对象引用mainwindow指针的巧妙方法 (***)
https://blog.youkuaiyun.com/ken2232/article/details/129638483


Qt里通过传递函数指针实现动态绑定信号/槽 (**)
https://blog.youkuaiyun.com/ken2232/article/details/131566404

----------------------------------------------------

================================

Qt里通过传递函数指针实现动态绑定信号/槽 (**)

// test.h

#include <QObject>

class QTimer;

class Test:public QObject

{

Q_OBJECT

public:

    Test(QObject * parent = 0);

    ~Test();

    void startTimer(int interval);

    void connectQTimer(void(Test::*pf)());

         

public slots:

    void onTimeout();

    void onPrintOut();

private:

    QTimer *m_timer;

};

#endif // TEST


// test.cpp

#include <QTimer>

#include <QDebug>

#include "test.h"


Test::Test(QObject *parent)

    : QObject(parent)

{

    m_timer = new QTimer(this);

}

Test::~Test()

{

    m_timer->deleteLater();

    m_timer = NULL;

}

void Test::startTimer(int interval)

{

    m_timer->start(interval);

    //    timer->setSingleShot(true);

}

                 

void Test::connectQTimer(void (Test::*pf)())

{

    connect(m_timer, &QTimer::timeout, this, pf);

    // 失败方法1

//    connect(m_timer, SIGNAL(timeout()),this, SLOT(pf));


    // 失败方法2

//    connect(m_timer, SIGNAL(timeout()),this, SLOT(*pf));


    // 失败方法3

//    connect(m_timer, SIGNAL(timeout()),this, SLOT((this->*pf)()));

                 

}

void Test::onTimeout()

{

    qDebug()<<"onTimeout()called...\n";

}

void Test::onPrintOut()

{

    qDebug()<<"onPrintOut()called...\n";

}

// main.cpp

#include <QCoreApplication>

#include <QTimer>

#include "test.h"


int main(int argc, char *argv[])

{

    QCoreApplication a(argc, argv);

    Test test;

         

    /*** 第一种方式传递函数指针 ***/

    test.connectQTimer(&Test::onTimeout);

    test.connectQTimer(&Test::onPrintOut);

        

    /*** 第二种方式传递函数指针 ***/

//    void(Test::*pf)() = &Test::onTimeout;

//    test.connectQTimer(pf);

//    void(Test::*pp)() = &Test::onPrintOut;

//    test.connectQTimer(pp);


    test.startTimer(5000);

    return a.exec();

}


————————————————
版权声明:本文为优快云博主「Prometheus2060」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/prometheus2060/article/details/44861725

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值