目录
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
462

被折叠的 条评论
为什么被折叠?



