Chinaunix首页 | 论坛 | 博客
  • 博客访问: 419545
  • 博文数量: 32
  • 博客积分: 1843
  • 博客等级: 上尉
  • 技术积分: 634
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-25 14:30
个人简介

Nothing is impossible

文章分类

全部博文(32)

文章存档

2013年(4)

2012年(13)

2011年(6)

2008年(9)

分类: C/C++

2012-12-10 16:28:20

目录

PART III  (槽与信号、串口)

9. 槽与信号
槽与信号是QT的一大特色,在QT编程中有广泛的应用,如QPushButton、QComboBox等等控件都会使用到。
常用的信号如下:

  1. //对于QLineEdit ,常用到信号textEdited,即当文本被编辑后,触发该信号
  2. QLineEdit *leoperator;
  3. QObject::connect(leoperator,SIGNAL(textEdited(QString)),this,SLOT(editoperator(QString)));

  4. //对于QPushButton,常用到信号clicked,即当按钮被按下后,触发该信号
  5. QPushButton *pbConnect;
  6. QObject::connect( pbConnect,SIGNAL(clicked()),this,SLOT(connect2port()) );

  7. //对于QComboBox,常用到信号currentIndexChanged,即当选择项改变时,触发该信号
  8. QComboBox *cbChipset;
  9. QObject::connect( cbChipset,SIGNAL(currentIndexChanged(int)),this,SLOT(chipsetchange(int)) );

  10. //对于QTimer,常用到信号timeout,即当计时器时间结束时,触发该信号
  11. QTimer tCount;
  12. QObject::connect(&tCount,SIGNAL(timeout()),this,SLOT(TimeCount()));

诸如此类,在QT控件中还有大量的SIGNAL,应参考QT帮助学习并使用,以上只是简单举例。

对于自定义类,同样可以自定义槽与信号,并使用之。
示例:

a. 自定义类

  1. class winSerial : public QThread
  2. {
  3.     Q_OBJECT

  4. public:
  5.     winSerial();
  6.      ~winSerial();

  7.    //此处省略多余代码
  8.    ....

  9. signals:
  10.     void DT_RDVD( QByteArray temp ); //Data recived
  11. };

b. 在主代码中定义SIGNAL

  1. ...
  2. private slots:
  3.     void ReadData(QByteArray temp);
  4. ...


c. 实例化winSerial并连接SIGNAL与SLOT

  1. winSerial *m_serial;
  2. QObject::connect( m_serial, SIGNAL(DT_RDVD(QByteArray)), this, SLOT(ReadData(QByteArray)));

d. 触发DT_RDVD
DT_RDVD只可以在自定义类中触发,只需在要触发的地方加入下面代码即可

  1. emit DT_RDVD( QByteArray(data)); // signal generated

按如上方法,即在触发SIGNAL(DT_RDVD)时,将传送一个QByteArray的值做为SLOT(ReadData)的实参,这样极大地方便了数据的传送


---------------------------------------------------------------------------------------------

10. 串口热拔插监听
关于串口热拔插监听,请更多地参考
《Qt中捕获Windows消息》
《USB转串口突然拔出检测解决方案》



代码

  1. #include <dbt.h>

  2. bool WndTest::winEvent( MSG * message, long * result )
  3. {
  4.     if( WM_DEVICECHANGE == message->message ){
  5.     }
  6.     else
  7.         goto ev_out;
  8.    
  9.     if( 0x8004 == message->wParam ){
  10.         DEV_BROADCAST_PORT *vol = (DEV_BROADCAST_PORT*)message->lParam;
  11.         QString str = QString::fromWCharArray(vol->dbcp_name); //获取要处理的串口名称

  12.         if( 3 != vol->dbcp_devicetype )
  13.             goto ev_out;
  14.        
  15.         if( m_serial ){
  16.             if( str.size() > 0 ){
  17.                 if( str.compare(cbComPort->currentText()) ==0 && m_connected ){ //比较串口名称,判定是否为当前使用的串口
  18.                     closeport(); //关闭串口
  19.                     qDebug()<<"close port:"<<str;
  20.                 }
  21.             }
  22.         }
  23.     }
  24.     if( 0x8000 == message->wParam ){
  25.         DEV_BROADCAST_PORT * vol = (DEV_BROADCAST_PORT*)message->lParam; //DEV_BROADCAST_VOLUME
  26.         QString str = QString::fromWCharArray(vol->dbcp_name);
  27.             if( str.size() > 0 ){
  28.                     qDebug()<<"to open port:"<<str; //在DEBUG信息中输出要处理的串口名称
  29.             }
  30.        
  31.     }
  32. ev_out:
  33.     return false;
  34. }

---------------------------------------------------------------------------------------------
阅读(8001) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~