한국어
Qt
 

QSocketNotifier 클래스는 파일 디스크립터의 활동 모니터링을 지원한다. 

리눅스 또는 유사 시스템에서 어떤 주변장치를 주기적으로 엑세스했던 경우 QSocketNotifier 클래스를 사용하면 간단하게 활동을 감지할 수 있다.

즉, 시스템콜 API를 호출하여 장치를 열고 socket notifier를 통해 파일 디스크립터를 모니터링 할 수 있다.

socket notifier는 해당 소켓 이벤트가 발생할 때마다 activated()시그널을 발생하며 이 스그널에 연결할 슬롯을 구현하면 된다. 

 

아래 간단한 예제 소스코드를 보자.

키보드 장치의 이벤트를 읽는 슬롯을 구현한 클래스이다.

#include <QObject>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>

class KbdHandler : public QObject
{
    Q_OBJECT
public:
    explicit KbdHandler(QObject *parent)
        : QObject(parent),
          m_fd(-1){
    }
    virtual ~KbdHandler(){
    }
    bool setFileDescriptor(int fd){
        if(fd < 0)
            return false;
        m_fd = fd;
        return true;
    }

    int fileDescriptor(){
        return m_fd;
    }

private Q_SLOTS:
    void readData(){
        input_event ev;
        if(read(m_fd, &ev, sizeof(input_event))){
            qDebug() << "code: " << ev.code;

            if(EV_KEY != ev.type)
                return;

            if(ev.value == 1){
                qDebug() << "down";
            }else{
                qDebug() << "up";
            }
        }
    }

private:
    int m_fd;
};

 

QSocketNotifier를 생성시에는 읽기, 쓰기 또는 둘 모두의 유형으로 설정해야한다.

Constant

Value

Description

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.

 

키보드 장치를 열고 파일 디스크립터 activated시그널과 readData슬롯을 연결한다. 이제 이 파일 디스크립터 활동이 감지되면 슬롯이 호출된다. 
KbdHandler *handler = new KbdHandler(&a);


if (handler->setFileDescriptor(open("/dev/input/event1", O_RDONLY | O_NONBLOCK, 0))) {
    qDebug() << handler->fileDescriptor();
    auto m_usbnotify = new QSocketNotifier(handler->fileDescriptor(), QSocketNotifier::Read);
    QObject::connect(m_usbnotify, SIGNAL(activated(int)), handler, SLOT(readData()));
}else{
    return -1;
}

.
.
.

// 사용이 끝나면 닫는 걸 잊지 않도록 한다.
close(handler->fileDescriptor());
delete handler;

 

Qt의 메인 이벤트 루프에서 검출되므로 별도의 이벤트 루프를 생성하지 않아도 된다.

번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 85892
179 Qt 응용프로그램에 Web 구성 요소를 표시 with Servo file makersweb 2024.04.27 32
178 Qt Creator 에서 GitHub Copilot 사용하기 file makersweb 2024.04.13 268
177 Qbs 프로젝트를 정의하기 위해 사용되는 몇가지 중요한 아이템들 file makersweb 2019.10.13 296
176 QtQuick 애플리케이션에 Rive 애니메이션 통합 makersweb 2024.03.31 319
175 Qt 6.4에 추가될 Qt Quick 3D Physics file makersweb 2022.08.07 343
174 HTTPS URL을 연결할 때 SslHandshakeFailedError 오류 makersweb 2022.07.31 358
173 Qt Android 앱에 AdMob 배너달기 file makersweb 2021.12.04 392
172 그래픽 소프트웨어에서 디자인 내보내기 (Exporting Designs from Graphics Software) j2doll 2020.12.25 413
171 Binding 타입으로 객체 속성 간 묶기 makersweb 2022.03.04 421
170 Base64로 인코딩된 파일을 복원 makersweb 2023.08.06 441
169 VirtualKeyboard 스타일 커스터 마이징 makersweb 2022.03.13 462
168 Qt Quick Controls 2에 네이티브 데스크탑 스타일 추가 file makersweb 2020.11.23 487
167 Android 애플리케이션 서명 구성 file makersweb 2023.12.17 491
166 앱을 종료할 때 QML 바인딩 오류를 피하는 방법 makersweb 2021.08.08 498
165 하드디스크 드라이브 여유 공간 계산 file makersweb 2023.01.15 502
164 안드로이드용 Qt 6.2 makersweb 2021.10.02 504
163 성능 고려 및 제안 사항 makersweb 2022.03.07 505
162 QRhi 에 대해서 file makersweb 2023.12.29 521
161 Qt Safe Renderer 개요 file makersweb 2022.09.08 544
160 QML의 사용자 정의 Image makersweb 2023.09.17 550