조회 수 9184 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

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의 메인 이벤트 루프에서 검출되므로 별도의 이벤트 루프를 생성하지 않아도 된다.


  1. No Image notice

    Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서

    Date2019.01.05 CategoryQML and Qt Quick By운영자 Views131086
    read more
  2. No Image

    컨테이너에 적재된 객체를 편리하게 삭제하기

    Date2019.09.18 CategoryGeneral and Desktop Bymakersweb Views7034
    Read More
  3. C++로 구현된 모델을 QML의 ListView에서 참조

    Date2019.09.07 CategoryQML and Qt Quick Bymakersweb Views10323
    Read More
  4. No Image

    QSocketNotifier로 파일 디스크립터의 활동감지

    Date2019.08.28 CategoryQML and Qt Quick Bymakersweb Views9184
    Read More
  5. No Image

    MCU용 Qt에 대해서

    Date2019.08.22 CategoryMobile and Embedded Bymakersweb Views7405
    Read More
  6. [Qt News] Qt for Python을 위한 기술 비전

    Date2019.08.20 CategoryGeneral and Desktop Byj2doll Views10127
    Read More
  7. No Image

    열거형(enum)을 QML에서 사용하는 방법과 문자열(QString)로 얻기

    Date2019.08.20 CategoryQML and Qt Quick Bymakersweb Views10760
    Read More
  8. No Image

    [Qt News] Qt 6 기술 비전 (Technical vision for Qt 6)

    Date2019.08.08 CategoryGeneral and Desktop Byj2doll Views8175
    Read More
  9. No Image

    [Qt News] Qt6 Git 개발 초기 단계 시작하기

    Date2019.08.02 CategoryInstallation and Deployment Byj2doll Views9604
    Read More
  10. No Image

    [Qt] Google Play의 향후 요구 사항을 준수하는 방법

    Date2019.07.29 CategoryQML and Qt Quick Byj2doll Views9160
    Read More
  11. No Image

    Qt기반의 오픈소스 프로젝트들 - 2

    Date2019.07.21 CategoryGeneral and Desktop By운영자 Views11739
    Read More
  12. No Image

    QML, 이미지, 폰트등을 바이너리 리소스로 만들기

    Date2019.06.24 CategoryInstallation and Deployment Bymakersweb Views8389
    Read More
  13. Qt Creator에서 임베디드 장치로 deploy설정(Custom Process Step)

    Date2019.06.15 CategoryInstallation and Deployment Bymakersweb Views9170
    Read More
  14. Qt Quick Controls 2사용 및 스타일 설정

    Date2019.06.07 CategoryQML and Qt Quick Bymakersweb Views11950
    Read More
  15. No Image

    QML 강좌 - 동적 Listing (ListView)

    Date2019.06.01 CategoryQML and Qt Quick Bymakersweb Views15143
    Read More
  16. QtInstallerFramework로 온라인 설치프로그램(Online Installer)만드는 방법

    Date2019.05.28 CategoryInstallation and Deployment Bymakersweb Views12624
    Read More
  17. QtCreator Design으로 GUI만들기 (QML로 만드는 Hello World -2)

    Date2019.05.26 CategoryQML and Qt Quick Bymakersweb Views23627
    Read More
  18. No Image

    QML에서 멀티 스레드(multithreading) 프로그래밍

    Date2019.05.25 CategoryQML and Qt Quick Bymakersweb Views8085
    Read More
  19. No Image

    QtSerialPort를 사용한 시리얼(Serial)통신

    Date2019.05.21 CategoryGeneral and Desktop Bymakersweb Views21344
    Read More
  20. No Image

    Qt기반의 오픈소스 프로젝트들

    Date2019.05.15 CategoryGeneral and Desktop Bymakersweb Views11057
    Read More
  21. No Image

    Q_D매크로와 d-pointer

    Date2019.05.07 CategoryGeneral and Desktop Bymakersweb Views7527
    Read More
Board Pagination Prev 1 4 5 6 7 8 9 Next
/ 9