Last active
January 6, 2025 23:53
-
-
Save gjorquera/2576569 to your computer and use it in GitHub Desktop.
Qt way to read from stdin.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is another example