Search code examples
c++qtqlineedit

Qt: How do I make loops that wait for user input on each iteration?


I'm not sure how to make the loop wait and iterate with a different input.

For example:

DO
{
// DO STUFF


}WHILE (Whatever is in lineEdit widget is not 'N') // User picks between Y and N

However, I can't seem to implement any way to wait at the end of the do part so that the user can edit the lineEdit text content.


Solution

  • In Qt, you would do nothing. Let the QApplication event loop do its thing. Just connect a handling slot to QLineEdit's textEdited(const QString & text ) signal.

    class MyObject : public QObject
    {
    Q_OBJECT
    public:
       MyObject();
       ~MyObject();
    
    private slots:
       void handleUserInput(const QString& text);
    
    private:
       QLineEdit* lineEdit_;
    };
    
    MyObject::MyObject()
       : lineEdit_(new QLineEdit)
    {
       connect(lineEdit_, SIGNAL(textEdited(const QString&)), 
               this, SLOT(handleUserInput(const QString&)));
    }
    
    MyObject::~MyObject()
    {
       delete lineEdit_;
    }
    
    void MyObject::handleUserInput(const QString& text)
    {
       if(text != "desiredText") 
          return;
    
       // do stuff here when it matches
    }