QComboBox background color - css

I have created a Qt HMI with QtDesigner and ui files. My QComboBox doesn't have the same background color in the designer and in real:
Designer:
Real life:
I am under Windows 7. Maybe it is OS dependent but I would like to have a white background.
I tried:
comboBox->setStyleSheet("QComboBox { background-color: white; }");
but it also paints the right arrow.
Any explanation?

Is the combo empty?
Try adding some elements and select one of them before running the "app".

Did you try changing QPalette::Base to white? You can do it without using any stylesheet.
QComboBox box = new QComboBox();
QPalette p = box.palette();
p.setColor(QPalette::Active, QPalette::Base, Qt::white);
p.setColor(QPalette::Inactive, QPalette::Base, Qt::white);
box.setPalette(p);

The QPalette::Base does not change the background of the QComboBox.
Instead I've used:
QPalette palette = ui->combo->palette();
palette.setColor(QPalette::Active, QPalette::Button, Qt::white);
palette.setColor(QPalette::Inactive, QPalette::Button, Qt::white);
ui->combo->setPalette(palette);
and it seems to work.

Related

Keeping the same selection color in a TableView whether it's active or not

I'm using QTableView class in my GUI and I would like the selected row to have the same color whether the TableView is active or inactive.
I tried to set this CSS stylesheet to achieve this:
QTableView:!active {
selection-background-color: palette(Highlight);
selection-color: palette(HighlightedText)
}
On Linux, it works just fine, but on Windows 7, when the TableView loses its focus, the text turns black instead of staying white (the background stays blue, so that part is OK). Am I missing something here ?
You also have to style text color, for example just add:
QTableView:!active {
...
selection-color: white;
}
This works well in python
pal = tbl_list.palette()
pal.setColor(QPalette.Inactive, QPalette.Highlight, pal.color(QPalette.Active, QPalette.Highlight))
tbl_list.setPalette(pal)

Background color of QComboBox does not change properly on windows 7

I am using QT (latest-5.6.0 msvc 64-bit) and want the comboBox to be displayed as white but it does not seem display correctly on win 7.
I have tried one or more combinations of the following:
QPalette p = ui.comboBox->palette();
p.setColor(QPalette::Active, QPalette::Button, Qt::white);
p.setColor(QPalette::Inactive, QPalette::Button, Qt::white);
p.setColor(QPalette::Active, QPalette::Background, Qt::white);
p.setColor(QPalette::Active, QPalette::Base, Qt::white);
p.setColor(QPalette::Inactive, QPalette::Background, Qt::white);
p.setColor(QPalette::Inactive, QPalette::Base, Qt::white);
p.setColor(ui.comboBox->backgroundRole(), Qt::white);
ui.comboBox->setPalette(p)
But none of them do change the display at least on win 7. I have also tried this:
ui.comboBox->setStyleSheet("QComboBox { background-color: white; }");
But the display became this:
Although it is white the down pointer has a grey box around it. I want it more like this:
Edit: I have tried this:
ui.comboBox->setStyleSheet("QComboBox { background-color: white; } QComboBox::drop-down { background-color: white; }");
but down arrow is not shown. How to change color while still showing the arrow?
Any solutions, suggestions, or ideas? Thanks.
Well currently, I found a solution. while it may not be the best, works which is to add a custom down arrow image (black arrow on a white background) and use it with stylesheet as follows:
ui.comboBox->setStyleSheet("
QComboBox { background-color: white; }
QComboBox::drop-down { image:url(:/Stock_Purchase_Simulator/Resources/arrow.png); }");

How to get the background color of QT-toolButton/pushButton using C++ [duplicate]

This question already has an answer here:
How to get widget background QColor
(1 answer)
Closed 6 years ago.
Related Qusetions:
How to get widget background QColor QWidget::palette().color(QWidget::backgroundRole())
How to get background color of a dialog/window?
Qt QPlainTextEdit background
There are at least 3 ways to solve the problem:
// 1st
QColor color = ui->toolButton->palette().color(QWidget::backgroundRole());
// 2nd
QColor color = ui->toolButton->palette().background().color();
// 3rd
QColor color = colorSetting = ui->toolButton->palette().color(QPalette::Window);
Update: sorry I have made some mistakes, both of the following ways work well.
Raw Qusetion:
I have tried
QColor color = ui->toolButton->palette().background().color();
and
QColor color = colorSetting = ui->toolButton->palette().color(QPalette::Window);
both got QColor(ARGB 1, 0.941176, 0.941176, 0.941176), not the right color I want.
The background color is set by editing mainwindow.ui, change stylesheet of toolButton
to background-color: rgb(255, 170, 255);
image of my toolButton
and for pyQt, see here How to get the background color of a button or label (QPushButton, QLabel) in PyQt
Your linked question is incorrect about the color role used by buttons. You are looking for QPalette::Button as your ColorRole.
QColor color = ui->toolbutton->palette()->color(QPalette::Button);
Be are that this color may not represent the background painted for the tool button. Some styles use gradients and QPalette stores brushes, not colors. Call QPalette::button() to retrieve the button background brush.
I suspect you intend to change the background color. You can call setBrush() to set it:
//Create a solid brush of the desired color
QBrush brush(someColor);
//Get a copy of the current palette to modify
QPalette pal = ui->toolbutton->palette();
//Set all of the color roles but Disabled to our desired color
pal.setBrush(QPalette::Normal, QPalette::Button, brush);
pal.setBrush(QPalette::Inactive, QPalette::Button, brush);
//And finally set the new palette
ui->toolbutton->setPalette(pal);

How to display a QColor from QColorDialog in a widget?

I have a ColorPicker dialog like:
QColor color = QColorDialog::getColor(Qt::black, this, "Pick a color", QColorDialog::DontUseNativeDialog);
The result of that I put in a QLineEdit via color.name() , e.g. #ff0000 .
I would like to display that color as the red field in this example, too
I don't know what Widget to pick for this to display? QPicture?
I enhanced this answer here. If you already grabbed the QColor in color, you can try for a QLabel* label:
QPalette palette = label->palette();
palette.setColor(label->backgroundRole(), color);
label->setAutoFillBackground(true);
label->setPalette(palette);

How to disable highlighting of widget?

In general I need to disable highlighting the QListWidget when user selects the item or widget receives the focus. But I think, that should be a common method for all widgets to do this.
You can set the palette for Highlight and HighlightedText roles. Just set the Highlight color of the widget to ‍‍‍‍Base‍‍‍ and HighlightedText to Text :
QPalette palette;
palette.setColor(QPalette::Highlight, listWidget->palette().color(QPalette::Base));
palette.setColor(QPalette::HighlightedText, listWidget->palette().color(QPalette::Text));
listWidget->setPalette(palette);
Alternatively you can do the following:
QListWidget lw;
[..]
QPalette p = lw.palette();
QColor bgColor = p.color(QPalette::Window);
QColor fgColor = p.color(QPalette::Text);
// Set the item selection color to be the background color of the list widget.
lw.setStyleSheet(QString("QListWidget:item:selected:active { background: %1;} "
"QListWidget:item:selected:active {color: %2; }")
.arg(bgColor.name()).arg(fgColor.name()));
If you mean highlight of (whole) QListView Widget, you may change this property in forms editor or set this in code:
listWidget->setFocusPolicy(Qt::NoFocus);

Resources