QPalette doesn't apply correctly with QHBoxLayout
-
Hi,
I have a custom widget that gets added to my main window. This custom widget simply contains a QHBoxLayout with two radio buttons and QLineEdit. However, the color palette doesn't seem to be working as expected on this custom widget.Header:
class myWidget : public QGroupBox { QHBoxLayout* _hbox = nullptr; QRadioButton* _rb1 = nullptr; QRadioButton* _rb2 = nullptr; QLineEdit* _lineEdit = nullptr; }
Implementation:
_hbox = new QHBoxLayout(); _hbox->setContentsMargins(0, 0, 0, 0); _hbox->setSpacing(5); _rb1 = new QRadioButton("RB1", _parent); _rb1->setChecked(true); _rb2 = new QRadioButton("RB2", _parent); _lineEdit = new QLineEdit(); _lineEdit->setText(tr("0")); _hbox->addWidget(_rb1); _hbox->addWidget(_rb2); _hbox->addWidget(_lineEdit); setLayout(_hbox); }
The layout is perfect. Now, I'm trying to highlight/change the color on the lineEdit in case it is found empty. I tried to use QPalette, it is working but the color is getting applied to the entire HBox layout including the radio buttons. I only want the color for the QLineEdit which is what I specified in the code below. It is straight forward code:
QPalette pal = _lineEdit->style()->standardPalette(); // also tried _lineEdit->palette(); pal.setColor(QPalette::Base, Qt::red); setPalette(pal);
What can possibly be wrong?
-
@vijaychsk said in QPalette doesn't apply correctly with QHBoxLayout:
What can possibly be wrong?
You set the palette to the whole widget, not only to the lineedit.
-
@Christian-Ehrlicher Good catch. I missed it. Thank you.