zoukankan      html  css  js  c++  java
  • QT--QSocketNotifier类介绍

     

     

    QSocketNotifier 用来监听系统文件操作,将操作转换为Qt事件进入系统的消息循环队列。并调用预先设置的事件接受函数,处理事件。

    一共存在3类事件:read,write,exception.

       
    QSocketNotifier::Read 0 There is data to be read.
    QSocketNotifier::Write 1 Data can be written.
    QSocketNotifier::Exception 2 An exception has occurred. We recommend against using this.

    每个QSocketNotifie对象只能监听一个事件,如果要同时监听两个以上事件必须创建两个以上的监听对象。

    QSocketNotifier::QSocketNotifier ( int socketType typeQObject * parent = 0 );

    下面将说明如何使用 QSocketNotifier 来监听串口数据:
    在使用 open 方法打开串口并设置好属性后,可以使用 Qt 的类 QSocketNotifier 来监听串口是否有数
    据可读,它是事件驱动的, 配合 Qt 的 signal/slot 机制,当有数据可读时,QSocketNotifier 就会发射
    ativated 信号,你只需要创建一个 slot 连接到该信号即可,代码如下所示:
    m_fd = openSerialPort();
    if (m_fd < 0)

    {
          QMessageBox::warning(this, tr("Error"), tr("Fail to open serial port!"));
         return ;
    }
    m_notifier = new QSocketNotifier(m_fd, QSocketNotifier::Read, this);
    connect (m_notifier, SIGNAL(activated(int)), this, SLOT(remoteDataIncoming()));


    在上述代码中,我们首先使用上面封装的 openSerialPort 函数打开串口并配置串口属性,接着我们
    用 m_fd 和 QSocketNotifier::Read 作为参数构造了一个 QSocketNotifier 的实例,
    QSocketNotifier::Read 参数表示我们需要关心串口的可读状态,最后将 QSocketNotifier 实例的
    activated 信号连接到 remoteDataIncoming slot,当有数据可读时,remoteDataIncoming slot 会被调
    用。
    下面是 remoteDataIncoming slot 的代码,它的代码比较简单,只是调用 read 函数读取串口数据,
    然后将数据显示到界面上:
    void TMainForm::remoteDataIncoming()
    {
    char c;
    if (read(m_fd, &c, sizeof c) != 1) {
    QMessageBox::warning(this, tr("Error"), tr("Receive error!"));
    return;
    }
    m_receiveEdit->insert(QString(QChar(c)));
    }
  • 相关阅读:
    FCN详解
    4、2支持向量机SVM算法实践
    Matplotlib
    4、Caffe其它常用层及参数
    6、TensorFlow基础(四)队列和线程
    0、weka学习与使用
    5、Tensorflow基础(三)神经元函数及优化方法
    4、TensorFlow基础(二)常用API与变量作用域
    elsa-core:4.ASP.NET Core Server with Elsa Dashboard
    elsa-core:3.elsa 服务
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/6564694.html
Copyright © 2011-2022 走看看