开发者

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 edit开发者_运维技巧ingFinished() 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?


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
  }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜