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. [SOLVED] QTextStream nonblocking peek and CLEAR '\n' from internal QTextStream buffer
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QTextStream nonblocking peek and CLEAR '\n' from internal QTextStream buffer

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.8k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    nicesw123
    wrote on last edited by
    #1

    Hi there,

    so I've started giving Qt a try.

    How well designed is QTextStream? Where is the fitting peek function?

    Here's what I want to do. Using the well-designed Standard C++ library, it works perfectly:
    @
    #include <iostream>
    #include <string>

    int main()
    {
    using namespace std;

    cout << "Enter a number: ";
    int i;
    cin >> i;
    if (!cin) {
    cerr << "err\n";
    return 1;
    }

    cout << "Getline: [enter for default "a b c"] ";
    string line;
    if (cin.peek() == '\n') {
    cin.ignore(1, '\n');
    }
    getline(cin, line);
    if (!cin) {
    cerr << "err\n";
    return 1;
    }

    if (line.size() == 0)
    line = "a b c";

    cout << "\n\n";
    cout << "Got number: " << i << " and the following line:\n"
    << line << '\n';

    return 0;
    }
    @

    But here's an attempt using Qt's QTextStream. It fails miserably. Please help.
    @
    #include <QTextStream>

    QTextStream cout_qt(stdout);
    QTextStream cin_qt(stdin);
    QTextStream cerr_qt(stderr);

    int main()
    {
    cout_qt << "Enter a number: " << flush;
    int i;
    cin_qt >> i;
    if (cin_qt.status() != QTextStream::Ok) {
    cerr_qt << "err\n";
    return 1;
    }

    cout_qt << "Getline: [enter for default "a b c"] ";
    QString line;

    /*
    HOW DOES ONE CLEAR '\n' FROM cin's BUFFER HERE???
    ============================
    */

    line = cin_qt.readLine();
    if (cin_qt.status() != QTextStream::Ok) {
    cerr_qt << "err\n";
    return 1;
    }

    cout_qt << "\n\n";
    cout_qt << "Got number: " << i << " and the following line:\n"
    << line << endl;

    return 0;
    }
    @

    I've already tried all kinds of things. cin_qt.skipWhiteSpace(), cin_qt.device()->peek(1), cin_qt.device()->bytesAvailable
    All to no avail...

    Is it even possible? Is the design flawed?

    Thanks.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chrisw01
      wrote on last edited by
      #2

      Hi

      You could try this....

      @
      #include <QTextStream>

      int main()
      {
      QTextStream cout_Qt(stdout);
      QTextStream cin_Qt(stdin);
      QTextStream cerr_Qt(stderr);
      QString string;
      qint32 number;

      cout_Qt << "Enter a Number: " << flush;
      number = cin_Qt.readLine().toInt();
      cout_Qt << "You entered : " << number << flush << "\n";
      
      cout_Qt << "Enter a string: " << flush;
      string = cin_Qt.readLine().toLatin1();
      cout_Qt << "You Entered : " << string << "\n" << flush;
      

      }
      @

      You may want to place the inputs into a loop until the parameters are met, such as
      @
      while((number < 1) && (number > 10)) {
      get your number code;
      }
      @

      Or this post may be of some help to you as well.. http://stackoverflow.com/questions/21401954/peek-on-qtextstream

      Oh, and don't quote me, Its been a while, but to get rid of the "\n" or any thing, I believe you can do

      @
      string = string.remove("\n");
      @

      Thanks

      1 Reply Last reply
      0
      • N Offline
        N Offline
        nicesw123
        wrote on last edited by
        #3

        OK I guess that after every use of...

        @
        cin_qt >> i;
        @

        ...I just have to remember to call
        @
        cin_qt.readLine(); // need this to eat the '\n' remaining in cin_qt's buffer
        @

        Here's the complete code

        @
        #include <QTextStream>

        QTextStream cout_qt(stdout);
        QTextStream cin_qt(stdin);
        QTextStream cerr_qt(stderr);

        int main()
        {
        cout_qt << "Enter a number: " << flush;
        int i;
        cin_qt >> i;
        if (cin_qt.status() != QTextStream::Ok) {
        cerr_qt << "err\n";
        return 1;
        }

        cin_qt.readLine();     // need this to eat the '\n' remaining in cin_qt's buffer
        
        cout_qt << "Getline: [enter for default \"a b c\"] " << flush;
        QString line;
        line = cin_qt.readLine();
        if (cin_qt.status() != QTextStream::Ok) {
            cerr_qt << "err\n";
            return 1;
        }
        
        if (line.size() == 0)
            line = "a b c";
        
        cout_qt << "\n\n";
        cout_qt << "Got number: " << i << " and the following line:\n"
                << line << endl;
        
        return 0;
        

        }
        @

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chrisw01
          wrote on last edited by
          #4

          Hi,

          That is one way to do it, however, I still recommend using @i = cin_qt.readLine();@

          as if for some reason
          @cin_qt >> i;@

          somehow manages to clear the buffer your program will be waiting for the user to hit the enter key with the next call to the blank readLine(). But I'm sure you got the idea now.

          Don't forget to edit your original post and put [SOLVED] in front of it so others will know.

          Thanks.

          1 Reply Last reply
          0

          • Login

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