emit信号发送结构体

来源:互联网 发布:mac输入法表情符号 编辑:程序博客网 时间:2024/04/28 13:16

这个是我结合网上是资料,整理出来的两中方法, 参考过的网页:http://blog.chinaunix.net/uid-28235086-id-3427874.html

屏蔽的是第一种方法: 数据很少,可以定义多个形参,像串口接收过来的数据这样的,直接使用即可。

没有屏蔽的是第二种方法, 自己定义结构体,结合QVariant 来使用。


[html] view plaincopy
  1. #ifndef CCOMCTROL_H  
  2. #define CCOMCTROL_H  
  3.   
  4. #include <QObject>  
  5. #include <QTimer>  
  6. typedef  unsigned char u8_t;  
  7. typedef  unsigned short u16_t;  
  8.  #include <QMetaType>  
  9. #include <QVariant>  
  10.   
  11. #pragma pack(push,1)  
  12. typedef struct __comRcvData_s  
  13. {  
  14.   u8_t len;  
  15.   u8_t *ptr;  
  16. }comRcvData_s;  
  17. #pragma pack(pop)  
  18.   
  19.   
  20. Q_DECLARE_METATYPE(comRcvData_s)//这里很重要不能漏  

  1. class CComCtrol : public QObject  
  2. {  
  3.     Q_OBJECT  
  4. public:  
  5.     explicit CComCtrol(QObject *parent = 0);  
  6.     ~CComCtrol();  
  7. signals:  
  8.    // void  rcvComData(const u8_t *data, int len);  
  9.     void rcvComData(QVariant dataVar);  
  10. public slots:  
  11.    void TimeOutProc();  
  12.   private:  
  13.        QTimer time1;  
  14.        int cnt;  
  15. };  
  16.   
  17. #endif // CCOMCTROL_H  

[html] view plaincopy
  1. #ifndef CTESTDEFINESIGNAL_H  
  2. #define CTESTDEFINESIGNAL_H  
  3.   
  4. #include <QtGui/QWidget>  
  5. #include "ccomctrol.h"  
  6. #include <QVariant>  
  7. class CTestDefineSignal : public QWidget  
  8. {  
  9.     Q_OBJECT  
  10.       
  11. public:  
  12.     CTestDefineSignal(QWidget *parent = 0);  
  13.     ~CTestDefineSignal();  
  14.   
  15. private slots:  
  16.    // void PrintRcvData(const u8_t *data,int len);  
  17.     void PrintRcvData(QVariant dataVar);  
  18. private:  
  19.     CComCtrol  *m_comCtrol;  
  20. };  
  21.   
  22. #endif // CTESTDEFINESIGNAL_H  

[html] view plaincopy
  1. #include "ccomctrol.h"  
  2.   
  3.   
  4. CComCtrol::CComCtrol(QObject *parent) :  
  5.     QObject(parent)  
  6. {  
  7.     comRcvData_s askData;  
  8.     QVariant DataVar;  
  9.     DataVar.setValue(askData);  
  10.     qRegisterMetaType<QVariant>("QVariant"); //写在构造函数里  
  11.   
  12.     connect(&time1,SIGNAL(timeout()),this,SLOT(TimeOutProc()));  
  13.     time1.start(200);  
  14. }  
  15. CComCtrol::~CComCtrol()  
  16. {  
  17. }  
  18. void CComCtrol::TimeOutProc()  
  19. {  
  20.     comRcvData_s rcvData;  
  21.     u8_t  buffer[10];  
  22.     rcvData.len=10;  
  23.     cnt+=10;  
  24.     for(int i=0;i<10;i++)  
  25.     {  
  26.         buffer[i]=cnt+i;  
  27.     }  
  28.     rcvData.ptr=buffer;  
  29.   
  30.        QVariant var1;  
  31.        var1.setValue(rcvData);  //设置发送的容器
  32.   
  33.   emit rcvComData(var1);  
  34. ///time1.stop();  
  35. }  

[html] view plaincopy
  1. #include "ctestdefinesignal.h"  
  2. #include <QDebug>  
  3.   
  4. CTestDefineSignal::CTestDefineSignal(QWidget *parent)  
  5.     : QWidget(parent)  
  6. {  
  7.     m_comCtrol=new CComCtrol;  
  8.   
  9.    // connect(m_comCtrol,SIGNAL(rcvComData(const u8_t *,int)),this,SLOT(PrintRcvData(const u8_t *,int)));  
  10.   
  11.     connect(m_comCtrol,SIGNAL(rcvComData(QVariant)),this,SLOT(PrintRcvData(QVariant)));  
  12. }  
  13.   
  14. CTestDefineSignal::~CTestDefineSignal()  
  15. {  
  16.       
  17. }  
  18. /*void CTestDefineSignal::PrintRcvData(const u8_t *data,int len)  
  19. {  
  20.     qDebug()<<"CTestDefineSignal::data len:"<<len;  
  21.     for(int i=0;i<len;i++)  
  22.     {  
  23.         qDebug()<<data[i];  
  24.     }  
  25. }  
  26. */  
  27. void CTestDefineSignal::PrintRcvData(QVariant dataVar)  
  28. {  
  29.     comRcvData_s askData;  
  30.     askData = dataVar.value<comRcvData_s>();  
  31.   
  32.     qDebug()<<"CTestDefineSignal::data len:"<<askData.len;  
  33.     for(int i=0;i<askData.len;i++)  
  34.     {  
  35.         qDebug()<<askData.ptr[i];  
  36.     }  
  37. }  

[html] view plaincopy
  1. #include <QtGui/QApplication>  
  2. #include "ctestdefinesignal.h"  
  3. #include "ccomctrol.h"  
  4. #include <QVariant>  
  5.   
  6.   
  7. int main(int argc, char *argv[])  
  8. {  
  9.     QApplication a(argc, argv);  
  10.     CTestDefineSignal w;  
  11.     w.show();  
  12.       
  13.     return a.exec();  
  14. }  
0 0
原创粉丝点击