mbaitoff
mbaitoff

Reputation: 9091

How to know which QLineEdit emitted the editingFinished() inside the signal handler?

I want to implement a custom response to user input for several similar QLineEdit objects. I want to create a common handler of editingFinished() or textChanged() signal and assign it to all the QLineEdits. However, the response requires the knowledge of the sender of the signal - for example, it must highlight the entered text with different colors.

How do I know the sender of the signal inside it's handler?

Upvotes: 2

Views: 3295

Answers (2)

user544511
user544511

Reputation: 759

You can get pointer to sender with call to QObject::sender() and then cast this pointer to QLineEdit. Something like

void MyClass::onTextChanged(const QString& text)
{
  QLineEdit* edit = qobject_cast<QLineEdit*>(sender());
  if (edit)
  {
    // Do something with QLineEdit
  }
  else
  {
    // Just to make sure that you have not make mistake with connecting signals
  }
}

Upvotes: 5

Johnny
Johnny

Reputation: 1974

May be you should consider using of QSignalMapper technique: http://doc.qt.io/qt-4.8/qsignalmapper.html

Upvotes: 0

Related Questions