Slot to color background of line edit on textedited
I have the following code to change the background of a line edit when it is edited by the user:
Code:
ProgramMain::ProgramMain() {
connect( ui.vendorname, SIGNAL( textEdited() ), this, SLOT( colorbackground() ) );
}
ProgramMain::colorbackground() {
ui.
vendorname->setStyleSheet
( QString( "background-color: yellow"));
}
This works, however I have many line edits in the UI file and I would like to have them all change color if the user edits them.
is there any way I can make a single slot that can handle any line edit? Or will I need to make a slot for each line edit that I want to change the color for?
Re: Slot to color background of line edit on textedited
1) Use QObject::sender() to identify the line edit being edited:
Code:
connect( lineEditA, SIGNAL( textEdited() ), this, SLOT(colorbackground() ) );
connect( lineEditB, SIGNAL( textEdited() ), this, SLOT(colorbackground() ) );
ProgramMain::colorbackground() {
QLineEdit* lineEdit
= qobject_cast<QLineEdit
*>
(sender
());
if (lineEdit)
lineEdit->setStyleSheet(...);
}
2) Use QSignalMapper
Re: Slot to color background of line edit on textedited
awesome! this was exactly what I was looking for :)
Re: Slot to color background of line edit on textedited
I put this code to action and I got the following error on the command line:
Quote:
no such signal QlineEdit::textEdited()
I looked in the docs and QlineEdit should have this signal. What could be missing?
Re: Slot to color background of line edit on textedited
Quote:
Originally Posted by
tpf80
I looked in the docs and QlineEdit should have this signal. What could be missing?
Looks like a parameter is missing. So it should be:
Code:
connect( ui.vendorname, SIGNAL( textEdited( const QString& ) ), this, SLOT( colorbackground() ) );
Re: Slot to color background of line edit on textedited
yes this was the issue.. so silly of me to forget that parameter! Thanks so much for your help!