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

这一生的挚爱 提交于 2019-12-11 03:35:59

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/5025089/how-to-know-which-qlineedit-emitted-the-editingfinished-inside-the-signal-hand

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!