한국어
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 118351
180 z-order 를 컨트롤 하기위한 방법 makersweb 2015.05.13 11483
179 Qml 사용자 ScrollBar 구현 file makersweb 2015.07.24 10306
178 QQuickImageProvider 를 이용한 Qml 에서 이미지 표시 makersweb 2015.10.18 9464
177 Qt의 시그널 슬롯 시스템 file makersweb 2015.10.20 29514
176 Qt의 스레드간 시그널 슬롯의 커넥션타입 [1] makersweb 2015.10.24 15702
175 Qt 프로그래밍의 시작 makersweb 2015.10.25 19800
174 QtConcurrent를 이용하여 쓰레드를 만드는 방법과 MapReduce file makersweb 2016.01.24 13559
173 Ubuntu Linux에서 Qt Creator 설치 file makersweb 2016.03.06 15740
172 멀티 스레드환경, 스레드에 안전한 이벤트처리 makersweb 2016.10.27 9300
171 QString 문자열에서 숫자만 추출해서 QString으로 반환 makersweb 2017.01.10 8968
170 Qt Logging Rule, Qt 프레임워크 로그 출력 makersweb 2017.01.13 9523
169 타임스탬프( timestamp) 유닉스 시간 makersweb 2017.10.19 7062
168 QPA 플러그인과 EGLFS file makersweb 2017.11.21 9734
167 QML에서 undefined를 확인하는 방법 makersweb 2017.11.29 6443
166 임의의 메모리 영역(QImage)에 QPainter를 이용하여 그리기 file makersweb 2017.12.19 7540
165 QPA 플러그인과 HTML5 Backend file makersweb 2017.12.27 5812
164 Qt 3D Studio 시작하기 file makersweb 2018.01.11 7136
163 Qt 어플리에이션 전역에 폰트 설정 makersweb 2018.01.24 10146
162 다국어 지원 어플리케이션 개발 file makersweb 2018.01.27 7191
161 Windows환경에서 mingw로 Qt 5.10 정적(static)빌드 file makersweb 2018.02.01 9470