In console how to prevent from going to the next line and read text stream
-
Below is my code for simple signal - slot example, but it seems that when I press enter it move to next line rather than reading the input.
Main.cpp file -
#include <QCoreApplication> #include "userinteractor.h" #include "chromebrowser.h" #include <QObject> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); ChromeBrowser chrome; UserInteractor interactor; QObject::connect(&interactor, &UserInteractor::phraseTyped, &chrome, &ChromeBrowser::browser); interactor.getInput(); return a.exec(); }UserInteractor.cpp file -
#include "userinteractor.h" #include <QDebug> #include <QTextStream> #include <QString> UserInteractor::UserInteractor(QObject *parent) : QObject{parent} { } void UserInteractor::getInput() { qDebug() << "\nEnter the phrase you are searching for: "; QTextStream str(stdin); const QString &phrase = str.readLine(); if (!phrase.isEmpty()) { emit phraseTyped(phrase); } }userinteractor.h file -
#ifndef USERINTERACTOR_H #define USERINTERACTOR_H #include <QObject> #include <QTextStream> class UserInteractor : public QObject { Q_OBJECT public: explicit UserInteractor(QObject *parent = nullptr); void getInput(); signals: void phraseTyped(const QString &phrase); }; #endif // USERINTERACTOR_HChromeBrowser.cpp file -
#include "chromebrowser.h" #include <QDebug> ChromeBrowser::ChromeBrowser(QObject *parent) : QObject{parent} { } void ChromeBrowser::browser(const QString &phrase) { qDebug() << "\nThe result for " << phrase << " are - " << "\n Result 1" << "\n Result 2" << "\n Result 3"; }chromebrowser.h file -
#ifndef CHROMEBROWSER_H #define CHROMEBROWSER_H #include <QObject> #include <QString> class ChromeBrowser : public QObject { Q_OBJECT public: explicit ChromeBrowser(QObject *parent = nullptr); public slots: void browser(const QString &phrase); }; #endif // CHROMEBROWSER_HConsole -

Here, I have pressed enter so many times but still no result is displayed.
-
Below is my code for simple signal - slot example, but it seems that when I press enter it move to next line rather than reading the input.
Main.cpp file -
#include <QCoreApplication> #include "userinteractor.h" #include "chromebrowser.h" #include <QObject> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); ChromeBrowser chrome; UserInteractor interactor; QObject::connect(&interactor, &UserInteractor::phraseTyped, &chrome, &ChromeBrowser::browser); interactor.getInput(); return a.exec(); }UserInteractor.cpp file -
#include "userinteractor.h" #include <QDebug> #include <QTextStream> #include <QString> UserInteractor::UserInteractor(QObject *parent) : QObject{parent} { } void UserInteractor::getInput() { qDebug() << "\nEnter the phrase you are searching for: "; QTextStream str(stdin); const QString &phrase = str.readLine(); if (!phrase.isEmpty()) { emit phraseTyped(phrase); } }userinteractor.h file -
#ifndef USERINTERACTOR_H #define USERINTERACTOR_H #include <QObject> #include <QTextStream> class UserInteractor : public QObject { Q_OBJECT public: explicit UserInteractor(QObject *parent = nullptr); void getInput(); signals: void phraseTyped(const QString &phrase); }; #endif // USERINTERACTOR_HChromeBrowser.cpp file -
#include "chromebrowser.h" #include <QDebug> ChromeBrowser::ChromeBrowser(QObject *parent) : QObject{parent} { } void ChromeBrowser::browser(const QString &phrase) { qDebug() << "\nThe result for " << phrase << " are - " << "\n Result 1" << "\n Result 2" << "\n Result 3"; }chromebrowser.h file -
#ifndef CHROMEBROWSER_H #define CHROMEBROWSER_H #include <QObject> #include <QString> class ChromeBrowser : public QObject { Q_OBJECT public: explicit ChromeBrowser(QObject *parent = nullptr); public slots: void browser(const QString &phrase); }; #endif // CHROMEBROWSER_HConsole -

Here, I have pressed enter so many times but still no result is displayed.
@amitsgh So, have you checked that your signal is being emitted?
This line:
const QString &phrase = str.readLine();is probably not what you think it is. QTextStream::readline() returns a QString
BTW: Do you get compiler warnings compiling UserInteractor.cpp?
-
@amitsgh So, have you checked that your signal is being emitted?
This line:
const QString &phrase = str.readLine();is probably not what you think it is. QTextStream::readline() returns a QString
BTW: Do you get compiler warnings compiling UserInteractor.cpp?
-
A amitsgh has marked this topic as solved on