这个是我结合网上是资料,整理出来的两中方法, 参考过的网页:http://blog.chinaunix.net/uid-28235086-id-3427874.html
屏蔽的是第一种方法: 数据很少,可以定义多个形参,像串口接收过来的数据这样的,直接使用即可。
没有屏蔽的是第二种方法, 自己定义结构体,结合QVariant 来使用。
#ifndef CCOMCTROL_H #define CCOMCTROL_H #include <QObject> #include <QTimer> typedef unsigned char u8_t; typedef unsigned short u16_t; #include <QMetaType> #include <QVariant> #pragma pack(push,1) typedef struct __comRcvData_s { u8_t len; u8_t *ptr; }comRcvData_s; #pragma pack(pop) Q_DECLARE_METATYPE(comRcvData_s) class CComCtrol : public QObject { Q_OBJECT public: explicit CComCtrol(QObject *parent = 0); ~CComCtrol(); signals: // void rcvComData(const u8_t *data, int len); void rcvComData(QVariant dataVar); public slots: void TimeOutProc(); private: QTimer time1; int cnt; }; #endif // CCOMCTROL_H
#ifndef CTESTDEFINESIGNAL_H #define CTESTDEFINESIGNAL_H #include <QtGui/QWidget> #include "ccomctrol.h" #include <QVariant> class CTestDefineSignal : public QWidget { Q_OBJECT public: CTestDefineSignal(QWidget *parent = 0); ~CTestDefineSignal(); private slots: // void PrintRcvData(const u8_t *data,int len); void PrintRcvData(QVariant dataVar); private: CComCtrol *m_comCtrol; }; #endif // CTESTDEFINESIGNAL_H
#include "ccomctrol.h" CComCtrol::CComCtrol(QObject *parent) : QObject(parent) { comRcvData_s askData; QVariant DataVar; DataVar.setValue(askData); qRegisterMetaType<QVariant>("QVariant"); //写在构造函数里 connect(&time1,SIGNAL(timeout()),this,SLOT(TimeOutProc())); time1.start(200); } CComCtrol::~CComCtrol() { } void CComCtrol::TimeOutProc() { comRcvData_s rcvData; u8_t buffer[10]; rcvData.len=10; cnt+=10; for(int i=0;i<10;i++) { buffer[i]=cnt+i; } rcvData.ptr=buffer; QVariant var1; var1.setValue(rcvData); emit rcvComData(var1); ///time1.stop(); }
#include "ctestdefinesignal.h" #include <QDebug> CTestDefineSignal::CTestDefineSignal(QWidget *parent) : QWidget(parent) { m_comCtrol=new CComCtrol; // connect(m_comCtrol,SIGNAL(rcvComData(const u8_t *,int)),this,SLOT(PrintRcvData(const u8_t *,int))); connect(m_comCtrol,SIGNAL(rcvComData(QVariant)),this,SLOT(PrintRcvData(QVariant))); } CTestDefineSignal::~CTestDefineSignal() { } /*void CTestDefineSignal::PrintRcvData(const u8_t *data,int len) { qDebug()<<"CTestDefineSignal::data len:"<<len; for(int i=0;i<len;i++) { qDebug()<<data[i]; } } */ void CTestDefineSignal::PrintRcvData(QVariant dataVar) { comRcvData_s askData; askData = dataVar.value<comRcvData_s>(); qDebug()<<"CTestDefineSignal::data len:"<<askData.len; for(int i=0;i<askData.len;i++) { qDebug()<<askData.ptr[i]; } }
#include <QtGui/QApplication> #include "ctestdefinesignal.h" #include "ccomctrol.h" #include <QVariant> int main(int argc, char *argv[]) { QApplication a(argc, argv); CTestDefineSignal w; w.show(); return a.exec(); }