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

### 如何在Qt5中使用函数指针实现信号的绑定 在Qt5中,可以通过现代C++风格的方式使用函数指针实现信号的绑定。这种方式相比传统的`SIGNAL`和`SLOT`宏更加安全且易于维护。以下是具体方法以及示例代码。 #### 方法说明 在Qt5中,推荐使用基于lambda表达式或者成员函数指针的方式来连接信号。对于成员函数指针的情况,语法如下: ```cpp connect(sender, &Sender::signal, receiver, &Receiver::slot); ``` 其中: - `sender` 是发出信号的对象指针。 - `&Sender::signal` 表示信号的具体名称,需通过取地址操作符获取[^1]。 - `receiver` 是接收信号的对象指针。 - `&Receiver::slot` 表示函数的具体名称,同样需要通过取地址操作符获取[^1]。 这种写法的优点在于编译期即可验证信号的匹配性,从而减少运行时错误的发生。 #### 示例代码 以下是一个完整的例子,展示如何使用函数指针方式完成信号的绑定: ```cpp #include <QApplication> #include <QPushButton> #include <QWidget> #include <QDebug> class MyWidget : public QWidget { Q_OBJECT public: MyWidget(QWidget* parent = nullptr) : QWidget(parent) { QPushButton* button = new QPushButton("Click Me", this); // 使用函数指针方式连接信号 connect(button, &QPushButton::clicked, this, &MyWidget::onButtonClicked); } private slots: void onButtonClicked() { qDebug() << "Button was clicked!"; } }; int main(int argc, char* argv[]) { QApplication app(argc, argv); MyWidget widget; widget.show(); return app.exec(); } #include "main.moc" ``` 在此示例中: - `button->clicked()` 是由 `QPushButton` 提供的标准信号[^2]。 - `MyWidget::onButtonClicked` 是自定义的函数。 - 连接语句 `connect(button, &QPushButton::clicked, this, &MyWidget::onButtonClicked)` 明确指出了信号之间的关系,并采用了函数指针的形式。 #### 解除绑定 如果需要解除特定对象上的所有信号绑定,可以调用该对象的 `disconnect()` 函数。例如,在某些情况下可能希望清理资源或重新配置连接,则可执行以下操作: ```cpp myObject->disconnect(); ``` 这会移除目标对象的所有现有连接[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值