Should QFileSystemWatcher work with device nodes?

476 views Asked by At

Qt is new to me so I don't know all the idioms yet. I have a custom linux driver which exposes itself as /dev/mydevice. When something interesting happens in hardware, the driver writes some data to that file. I have tested that this works with

xxd -l 16 /dev/mydevice

and can see my data being dumped to the screen when I press a button.

Now I want a simple GUI to show me what's being dumped; QFileSystemWatcher seems like a good candidate since it "monitors the file system for changes to files," but it doesn't fire the fileChanged signal.

I'm guessing QFileSystemWatcher is just looking at the modification time or something like that? Since QFile doesn't implement the readyRead signal, am I down to spawning a new thread and looping on QFile::read()? Or implementing my own QIODevice that does that? What's the best way to achieve my goal?

Here is the toy example.

main.cpp:

#include <QCoreApplication>
#include <QDebug>
#include <QFileSystemWatcher>
#include <QString>

class EventTester : public QObject
{
    Q_OBJECT
public:
    EventTester(QObject *parent = 0) : QObject(parent)
    {
        qfsw = new QFileSystemWatcher(this);

        if (!qfsw->addPath("/dev/mydevice")) {
            qDebug() << "Couldn't add watcher.";
        }
        connect(qfsw, &QFileSystemWatcher::fileChanged,
                this, &EventTester::onEvent);
    }

    QFileSystemWatcher *qfsw;

public slots:
    void onEvent(const QString &path)
    {
        Q_UNUSED(path);
        qDebug() << "We got a special event!";
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    EventTester e();

    return a.exec();
}

qfswtestcon.pro:

QT       += core

QT       -= gui

TARGET = qfswtestcon
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp
0

There are 0 answers