Skip to content

Instantly share code, notes, and snippets.

@gjorquera
Last active February 4, 2024 12:44
Show Gist options
  • Save gjorquera/2576569 to your computer and use it in GitHub Desktop.
Save gjorquera/2576569 to your computer and use it in GitHub Desktop.
Qt way to read from stdin.
#include <iostream>
#include "console.hpp"
void Console::run()
{
std::cout << "First message" << std::endl;
std::cout << "> " << std::flush;
connect(m_notifier, SIGNAL(activated(int)), this, SLOT(readCommand()));
}
void Console::readCommand()
{
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;
}
}
#pragma once
#include <QObject>
#include <QSocketNotifier>
#include <iostream>
class Console : public QObject
{
Q_OBJECT;
public:
Console();
void run();
signals:
void quit();
private:
QSocketNotifier *m_notifier;
private slots:
void readCommand();
};
inline Console::Console()
{
m_notifier = new QSocketNotifier(fileno(stdin), QSocketNotifier::Read, this);
}
#include "console.hpp"
#include <QCoreApplication>
int main(int argc, char** argv)
{
QCoreApplication app(argc, argv);
Console console;
console.run();
QObject::connect(&console, SIGNAL(quit()), &app, SLOT(quit()));
return app.exec();
}
@spartawhy117
Copy link

@Alexan14 i have the same problem, have you solved it?

@hao-lee
Copy link

hao-lee commented Dec 6, 2017

Awesome! Very useful! It works well on Linux with Qt 5.6.

@olned
Copy link

olned commented Dec 23, 2017

Thanks!
@spartawhy117, to support Windows take a look at this example.
I just added a few lines to the proposed code:

#ifdef Q_OS_WIN
#include <QWinEventNotifier>
#include <windows.h>
#else
#include <QSocketNotifier>
#endif
...
#ifdef Q_OS_WIN
    QWinEventNotifier *m_notifier;
#else
    QSocketNotifier *m_notifier;
#endif
...
#ifdef Q_OS_WIN
    m_notifier = new QWinEventNotifier(GetStdHandle(STD_INPUT_HANDLE));
    connect(m_notifier, &QWinEventNotifier::activated
#else
    m_notifier = new QSocketNotifier(fileno(stdin), QSocketNotifier::Read, this);
    connect(m_notifier, &QSocketNotifier::activated
#endif
        , this, &Console::readCommand);

@sequency
Copy link

sequency commented Mar 9, 2018

Thanks, exact what I needed.
Just one thing I had to change, in console.hpp line 9:
Q_OBJECT <- no semicolon goes here
Using Qt 5.10 on openSuSE 42.3 and raspbian jessie

@juangburgos
Copy link

Hi guys,

I updated this to Qt5 and C++11 and put it in a repo so it can be used as a git submodule:

You can find it here.

Cheers,

@mayankgowda
Copy link

Thanks a lot man. It was exactly what i was looking for.

@AndreiCherniaev
Copy link

There is another example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment