Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. In console how to prevent from going to the next line and read text stream
Forum Updated to NodeBB v4.3 + New Features

In console how to prevent from going to the next line and read text stream

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 251 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    amitsgh
    wrote on last edited by
    #1

    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_H
    
    

    ChromeBrowser.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_H
    
    

    Console -
    console pic

    Here, I have pressed enter so many times but still no result is displayed.

    C 1 Reply Last reply
    0
    • A amitsgh

      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_H
      
      

      ChromeBrowser.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_H
      
      

      Console -
      console pic

      Here, I have pressed enter so many times but still no result is displayed.

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      @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 1 Reply Last reply
      1
      • C ChrisW67

        @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 Offline
        A Offline
        amitsgh
        wrote on last edited by
        #3

        @ChrisW67 I think I got it, actually earlier it was opening the Application output console, I just clicked the option of run in terminal in the build & run option and now it's working fine.

        1 Reply Last reply
        1
        • A amitsgh has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved