한국어
Qt
 

사용자로부터 콘솔창을 통해 입력받기 위한 예제소스코드이다.

#ifdef Q_OS_WIN
#include <QWinEventNotifier>
#include <windows.h>
#else
#include <QSocketNotifier>
#endif

#include <iostream>

class Console : public QObject
{
    Q_OBJECT
public:
    Console(QObject *parent = nullptr):QObject(parent){}

    void run()
    {
        std::cout << "Please input command." << std::endl;
        std::cout << "> " << std::flush;

#ifdef Q_OS_WIN
        m_notifier = new QWinEventNotifier(GetStdHandle(STD_INPUT_HANDLE));
        connect(m_notifier, &QWinEventNotifier::activated, [&](HANDLE) {
#else
        m_notifier = new QSocketNotifier(fileno(stdin), QSocketNotifier::Read, this);
        connect(m_notifier, &QSocketNotifier::activated, [&](int) {
#endif
            std::string line;
            std::getline(std::cin, line);
            if (std::cin.eof() || line == "quit") {
                std::cout << "Good bye!" << std::endl;
                emit quit();
            } else {
                std::cout << "Echo: " << line << std::endl;
                std::cout << "> " << std::flush;
            }
        });
    }

private slots:

signals:
    void quit();

private:
#ifdef Q_OS_WIN
    QWinEventNotifier *m_notifier;
#else
    QSocketNotifier *m_notifier;
#endif
};

 

main.cpp

#include <QCoreApplication>

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

    Console console;
    console.run();

    // quit 시그널을 수신하면 종료
    QObject::connect(&console, SIGNAL(quit()), &a, SLOT(quit()));

    return a.exec();
}

 

결과:
input_qt_app.png
번호 제목 글쓴이 날짜 조회 수
공지 Qt프로그래밍(QtQuick) Beginner를 위한 글 읽는 순서 운영자 2019.01.05 116910
120 싱글터치 스크린 및 임베디드 리눅스 기반에서 Qt 터치입력 makersweb 2018.12.24 5161
119 Qbs에 대한 소개와 설치하는 방법 makersweb 2019.10.09 5169
118 최초의 Qt 6.0 스냅샷 제공 (First Qt 6.0 Snapshot Available) j2doll 2020.06.21 5173
117 웹기반 Qt Design Viewer [2] file makersweb 2019.10.23 5207
116 QRandomGenerator 클래스를 사용하여 난수(random values) 생성 makersweb 2020.10.17 5228
115 Q_D매크로와 d-pointer file makersweb 2019.05.07 5255
114 Qt기반의 서버와 클라이언트간 SOAP(Simple Object Access Protocol) file makersweb 2020.05.11 5292
113 clazy 로 13개의 시그널, 슬롯 오류 해결 makersweb 2022.08.23 5352
112 Qml에서 커튼효과 구현 예제 - Shader Effects file 운영자 2018.12.05 5391
111 QPA 플러그인과 HTML5 Backend file makersweb 2017.12.27 5435
110 QThread 및 QMutex 예제 makersweb 2021.01.12 5440
109 VTK 를 사용해서 강력한 시각화(3D, Plotting, Chart)Qt 응용프로그램 개발하기 file makersweb 2022.10.16 5548
108 Qt3D의 QML 타입으로 3D렌더링 file makersweb 2019.11.20 5560
107 Loader를 사용하여 동적으로 QML 로드 makersweb 2021.01.19 5587
106 [Qt] Google Play의 향후 요구 사항을 준수하는 방법 [2] j2doll 2019.07.29 5588
105 앱을 종료할 때 QML 바인딩 오류를 피하는 방법 makersweb 2021.08.08 5651
104 QML의 사용자 정의 Image makersweb 2023.09.17 5748
103 안드로이드용 Qt 6.2 makersweb 2021.10.02 5772
102 싱글 샷(Single-Shot) 시그널/슬롯 연결 makersweb 2021.05.12 5773
101 Qt5기반 독립 프로세스(out-of-process)로 동작하는 가상키보드(virtual keyboard) file makersweb 2019.02.24 5788