#include <QObject>
#include <QDebug>
class MyProcess : public QObject
{
Q_OBJECT
public:
explicit MyProcess(QObject *parent = nullptr):QObject(parent)
{
connect(this, QOverload<int>::of(&MyProcess::sigMsg),this, QOverload<int>::of(&MyProcess::onMsg));
connect(this, QOverload<const QString &>::of(&MyProcess::sigMsg),this, QOverload<const QString &>::of(&MyProcess::onMsg));
connect(this, QOverload<int,const QString &>::of(&MyProcess::sigMsg),this, QOverload<int,const QString &>::of(&MyProcess::onMsg));
emit sigMsg(1);
emit sigMsg("msg");
emit sigMsg(2,"test");
}
~MyProcess()
{
}
signals:
void sigMsg(int);
void sigMsg(const QString&);
void sigMsg(int,const QString&);
private slots:
void onMsg(int msg)
{
qDebug()<<" msg:"<<msg;
}
void onMsg(const QString &msg)
{
qDebug()<<" msg:"<<msg;
}
void onMsg(int msg1,const QString &msg2)
{
qDebug()<<" msg1:"<<msg1<<",msg2:"<<msg2;
}
};
qt 帮助文档中,connect函数显示的接口传参格式:
[static] QMetaObject::Connection QObject::connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type = Qt::AutoConnection)
qt帮助文档表明,connect传入信号函数和槽函数的函数指针作为参数。
思路:利用qt的QOverload<>::of方法指定重载函数的函数指针。