PDA

View Full Version : Slot to color background of line edit on textedited



tpf80
20th June 2007, 21:25
I have the following code to change the background of a line edit when it is edited by the user:




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?

jpn
20th June 2007, 21:49
1) Use QObject::sender() to identify the line edit being edited:


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

tpf80
20th June 2007, 21:53
awesome! this was exactly what I was looking for :)

tpf80
21st June 2007, 01:08
I put this code to action and I got the following error on the command line:


no such signal QlineEdit::textEdited()

I looked in the docs and QlineEdit should have this signal. What could be missing?

jpn
21st June 2007, 08:16
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:

connect( ui.vendorname, SIGNAL( textEdited( const QString& ) ), this, SLOT( colorbackground() ) );

tpf80
21st June 2007, 09:02
yes this was the issue.. so silly of me to forget that parameter! Thanks so much for your help!