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 QLineEdit
s. 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
精彩评论