Reputation: 9091
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 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?
Upvotes: 2
Views: 3295
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
Reputation: 1974
May be you should consider using of QSignalMapper technique: http://doc.qt.io/qt-4.8/qsignalmapper.html
Upvotes: 0