I think you have the logic a little inside-out. It is the line edits that should react to the signal sent from somewhere else. In your MainWindow class (or whoever owns the line edits), you implement both the slot for setting the new palette on all of the line edits as well as the signal that gets emitted when a new palette should be set on a line edit.
// constructor
{
// usual setup stuff here
connect( this, &MainWindow::paletteChanged, this, &MainWindow::onPaletteChanged );
}
// slot pseudocode; slot declared in "slots" section of class definition
void MainWindow::onChooseColor()
{
if ( newColor.isValid() )
{
palette.
setColor( QPalette::Window, newColor
);
emit paletteChanged( newPalette ); // Your signal, defined in "signals" section of MainWindow class definition
}
}
// slot pseudocode; slot declared in "slots" section of class definition
void MainWindow
::onPaletteChanged( const QPalette & newPalette
) {
{
if ( pEdit->palette() != newPalette )
pEdit->setPalette( newPalette );
}
}
// constructor
MainWindow::MainWindow( QWidget * parent ) : QMainWindow( parent )
{
// usual setup stuff here
connect( this, &MainWindow::paletteChanged, this, &MainWindow::onPaletteChanged );
}
// slot pseudocode; slot declared in "slots" section of class definition
void MainWindow::onChooseColor()
{
QColor newColor = QColorDialog::getColor( ... );
if ( newColor.isValid() )
{
QPalette newPalette;
palette.setColor( QPalette::Window, newColor );
emit paletteChanged( newPalette ); // Your signal, defined in "signals" section of MainWindow class definition
}
}
// slot pseudocode; slot declared in "slots" section of class definition
void MainWindow::onPaletteChanged( const QPalette & newPalette )
{
foreach( QLineEdit * pEdit in ui )
{
if ( pEdit->palette() != newPalette )
pEdit->setPalette( newPalette );
}
}
To copy to clipboard, switch view to plain text mode
One way to do it, assuming you want all of the line edits to have the same color. Why you'd want to do that, I don't know. Usually color changes are used to indicate specific things about specific items in a UI, like a red background to indicate an error in a line edit entry. You don't change all of them to red, just the bad ones. One the other hand, if you are providing an app that has custom skinning, then this would make sense in that context.
You cannot avoid looping through the line edits to set each one's palette. If you derive from QLineEdit and implement a custom signal / slot combination on the derived class to do this, you still, at some point, have to loop over all of the line edit instances to hook up the signal and slot for all of them. Nothing is going to make a QWidget magically respond to some external change unless you tell it to, and that's by way of a signal / slot connection or event handler.
Bookmarks