Using QSocketNotifier to select on a char device.

后端 未结 2 1046
一整个雨季
一整个雨季 2020-12-31 16:05

I wrote a char device driver and am now writing a QT \"wrapper\" which part of it is to get a signal to fire when the device becomes readable via the poll mechanism. I had

相关标签:
2条回答
  • 2020-12-31 16:44

    Your QSocketNotifer gets destroyed as soon as that if block ends. It doesn't stand a chance of reporting anything.

    You must keep that socket notifier alive as long as you want that file to be monitored. The simplest way of doing that is probably keeping a QSocketNotifer* member in one of your classes.

    0 讨论(0)
  • 2020-12-31 17:05

    I'll also mention that QSocketNotifier can be used to watch stdin using the following

    #include "ConsoleReader.h"
    #include <QTextStream>
    
    #include <unistd.h> //Provides STDIN_FILENO
    
    ConsoleReader::ConsoleReader(QObject *parent) :
        QObject(parent),
        notifier(STDIN_FILENO, QSocketNotifier::Read)
    {
        connect(&notifier, SIGNAL(activated(int)), this, SLOT(text()));
    }
    
    void ConsoleReader::text()
    {
        QTextStream qin(stdin);
        QString line = qin.readLine();
        emit textReceived(line);
    }
    

    ---Header

    #pragma once
    
    #include <QObject>
    #include <QSocketNotifier>
    
    class ConsoleReader : public QObject
    {
        Q_OBJECT
    public:
        explicit ConsoleReader(QObject *parent = 0);
    signals:
        void textReceived(QString message);
    public slots:
        void text();
    private:
        QSocketNotifier notifier;
    };
    
    0 讨论(0)
提交回复
热议问题