如何使用Qt在Linux中读取文件设备?

Ale*_*x44 5 c++ linux qt

我正在开发一个基于Qt5的小型GUI,它将显示来自Linux文件设备的数据流.为此,我选择了一个操纵杆输入.通过cat /dev/input/js0它可以在终端上看到传入的流.

使用C,您可以使用带阻塞读取的循环读取此设备文件或处理设备信号.但我不会用Qt得到这个.

使用Qt与设备文件交互的典型方法是什么?


根据@rodrigo的答案,这里有一个新的实现:

joystick.h

#ifndef JOYSTICK_H
#define JOYSTICK_H

#include <QObject>
#include <QFile>
#include <QSocketNotifier>

class Joystick
      : public QObject
{
    Q_OBJECT

    QString fileName = "/dev/input/js0";
    QFile *file;
    QSocketNotifier *notifier;

public:
    explicit Joystick(QObject *parent = nullptr);
    ~Joystick();

signals:

public slots:
    void handle_readNotification(int socket);
};

#endif // JOYSTICK_H
Run Code Online (Sandbox Code Playgroud)

joystick.cpp

#include "joystick.h"

Joystick::Joystick(QObject *parent)
   : QObject(parent)
{
    file = new QFile();
    file->setFileName(fileName);
    if( !file->exists() ){
        qWarning("file does not exist");
        return;
    }

    if( !file->open(QFile::ReadOnly) ){
        qWarning("can not open file");
        return;
    }

    notifier = new QSocketNotifier( file->handle(),
                                    QSocketNotifier::Read,
                                    this);

    connect( notifier,
             &QSocketNotifier::activated,
             this,
             &Joystick::handle_readNotification );

    if( !notifier->isEnabled() ){
        qInfo("enable notifier");
        notifier->setEnabled(true);
    }
    qInfo("Joystick init ready");
}

void
Joystick::handle_readNotification(int /*socket*/)
{
    static quint64 cnt=0;
    qInfo("cnt: %d",cnt++);

    if( !(file->isOpen()) ){
        qWarning("file closed");
        return;
    }

    char buf[16]; /* tested with different sizes */
    if( file->read(buf,sizeof(buf)) ){
        qInfo("read: %s",buf);
    }
//  QByteArray ba = file->readAll();
//  qInfo("Data: %s", ba.data());
}
Run Code Online (Sandbox Code Playgroud)

然后我运行这个,最后一个输出是cnt: 0.看起来,read或者readAll现在打电话阻止.如果我注释掉读取调用,计数器运行速度非常快.这里有一个类似的帖子这里错了吗?


最终解决方案

感谢rodrigo!

joystick.h

#ifndef JOYSTICK_H
#define JOYSTICK_H

#include <QObject>
#include <QFile>
#include <QSocketNotifier>

class Joystick
      : public QObject
{
    Q_OBJECT

    QString fileName = "/dev/input/js0";
    QSocketNotifier *notifier;
    int fd;

public:
    explicit Joystick(QObject *parent = nullptr);
    ~Joystick();

signals:
    void buttonPressed(quint8 number, qint16 value);
    void axisMoved(quint8 number, qint16 value);


public slots:
    void handle_readNotification(int socket);
};

#endif // JOYSTICK_H
Run Code Online (Sandbox Code Playgroud)

joystick.cpp

#include "joystick.h"

#include <fcntl.h>
#include <unistd.h>
#include <linux/joystick.h>

Joystick::Joystick(QObject *parent)
   : QObject(parent)
{
    auto file = new QFile();
    file->setFileName(fileName);
    if( !file->exists() ){
        qWarning("file does not exist");
        return;
    }

    fd = open(fileName.toUtf8().data(), O_RDONLY|O_NONBLOCK);
    if( fd==-1 ){
        qWarning("can not open file");
        return;
    }

    notifier = new QSocketNotifier( fd,
                                    QSocketNotifier::Read,
                                    this);

    connect( notifier,
             &QSocketNotifier::activated,
             this,
             &Joystick::handle_readNotification );
}

Joystick::~Joystick()
{
    if( fd>=0 ){
        close(fd);
    }
}

void
Joystick::handle_readNotification(int /*socket*/)
{
    struct js_event buf;
    while( read(fd,&buf,sizeof(buf))>0 ){
        switch (buf.type) {
        case JS_EVENT_BUTTON:
            emit buttonPressed(buf.number, buf.value);
            break;
        case JS_EVENT_AXIS:
            emit axisMoved(buf.number, buf.value);
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

rod*_*igo 6

通常使用工具包的轮询源解决此问题.在Qt的情况下,这是QSocketNotifier.尽管它的名字(历史事故?),它可用于轮询任何文件描述符,而不仅仅是套接字.

所以你只需打开设备,open()获取文件描述符,然后QSocketNotifier在其上创建一个类型QSocketNotifier::Read.当有要读取的事件时,您将获得activate()信号.