Louis93
Louis93

Reputation: 3913

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.

Upvotes: 0

Views: 2591

Answers (1)

cgmb
cgmb

Reputation: 4414

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
}

Upvotes: 3

Related Questions