How to disable highlighting of widget? - qt

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);

Related

How to change (remove) selection/active color of QListWidget

In my QListWidget, there are some items that have non-default background color, I set them like so inside the custom QListWidget class:
item->setBackgroundColor(qcolor); // item is of type QListWidgetItem*
Those non-default colors that I set are distorted by the QListWidget's selection color. See an example:
Items three and four are supposed to be the same color, but they are not since the item four is selected, and thus the result color is a summation of original color and QListWidget's selection (active item?) color.
My question is how to edit or remove that selection color?
I tried inside my QListWidget (in special slot when I want to change the item's background color):
QPalette pal = this->palette();
pal.setColor(QPalette::Highlight, QColor(255,255,255,0));
this->setPalette(pal); // updated
But it did not produce any effect. what am I doing wrong? Is it a correct variable to set? Do I set it up inside QListWidget or inside its delegate?
Update: I tried using stylesheets as pointed by comment/answer, however, it will not be possible to use them for my application, because the items in my rows have 3 states (so I would need to use three colors). E.g., 3 states that correspond to three colors: pink for active, green for inactive and gray for the rest. When using stylesheets, I cannot set the custom property (let's say QListWidget::item[Inactive="true"]) to a single QListWidgetItem, but for the full QListWidget, and therefore it colors all the rows the same color.
Stylesheets were tried for similar problem here, and didn't work, therefore I make conclusion using stylesheets is not the way to go.
The background change method that I used originally works fine for my purpose, but I cannot figure out how to get rid of the default selection color (transparent light blue) which adds to the background color and produces the mixed color.
I think you'd be better served using the style sheets to do this. Here's an example
QListWidget::item
{
background: rgb(255,255,255);
}
QListWidget::item:selected
{
background: rgb(128,128,255);
}
::item indicates the individual items within the QListWidget, while :selected indicates the QListWidgetItems which are currently selected.
To then get the custom background on specific widgets, you could use custom style sheet properties. In your code, call something like this on the widget you want to apply a custom style on:
myList->setProperty( "Custom", "true" );
// Updates the style.
style->unpolish( myList );
style->polish( myList );
Then in your style sheet, define the style for your custom property like so.
QListWidget::item[Custom="true"]
{
background: rgb(128,255,128);
}
I found a suitable solution by using a delegate. So, there is no need to use QPalette; and for my problem the stylesheets will not work. This solution will also work when different rows (QListWidget or QTreeWidget) are needed to be colored in different colors, depending on some state.
The background color is set as described on the question:
item->setBackgroundColor(qcolor); // change color slot inside QListWidget class
In order to define rules how the widget is painted, I re-define the delegate:
class Delegate : public QStyledItemDelegate {};
Then I re-define Delegate's method paint(). There I define how to draw each row of my widget. More specifically, I only call custom drawing when the mouse hovering over an item, or that item is in selected state (those are the conditions when the row is selected by the light blue color which I want to avoid). The code looks like this:
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if((option.state & QStyle::State_Selected) || (option.state & QStyle::State_MouseOver))
{
// get the color to paint with
QVariant var = index.model()->data(index, Qt::BackgroundRole);
// draw the row and its content
painter->fillRect(option.rect, var.value<QColor>());
painter->drawText(option.rect, index.model()->data(index, Qt::DisplayRole).toString());
}
else
QStyledItemDelegate::paint(painter, option, index);
// ...
}
Of course, do not forget to associate the QListWidget with Delegate:
listWidget->setItemDelegate(new Delegate());

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 change highlight color on QTreewidgetItem

I have wrote an app using QTreeWidget. Currently when selecting an item using the mouse, this item is highlighted in blue as probably default palette.
How can I change the property to modify the highlight color when a QTreeWidgetItem is selected using the mouse. For example, the color is no more blue but the one I want.
My TreeWidget is defined as below:
setSortingEnabled(true);
setColumnWidth(0, 400);
setExpandsOnDoubleClick(true);
setAlternatingRowColors(true);
QPalette p = palette();
p.setColor( QPalette::AlternateBase, QColor(0xef, 0xef, 0xef) );
setPalette(p);
setSelectionBehavior(QAbstractItemView::SelectRows);
setSelectionMode(QAbstractItemView::ExtendedSelection);
I have also subclass the QTreeWidgetItem but it's mostly to add parameter and change the font by default
MyTreeWidgetItem::MyTreeWidgetItem()
{
isFolder = false;
filename = NULL;
QFont ItemFont = QFont(FONT_TYPE);
ItemFont.setPointSize(FONT_SIZE_MEDIUM);
setFont(0,ItemFont);
}
Any idea ?
Thanks

QComboBox background color

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.

How to change color of QWidget in QTableWidget

I have a QTableWidget. In its cells I need to display 3-state QSliders, that must change their color, depending form their state. -1 = red, 0 - normal, 1 - green. I tried to set QPalette to QSlider - whitout success. I tried to place QSlider into QWidget with Layout and apply palette to QWidget - whitout success.
How to do that? I need any color sign (border or full background, e.t.c) How to do that?
You can use QItemDelegate, then you'll could to rule your QSlider into QTableWidget.
Detail.
At first, you should derive from QItemDelegate. A good docs presents by doc.qt.digia example using qitemdelegate
You should substitute QSpinBox to QSlider. And after reading this document, you can do needed with setting color your QSlider.
QTableWidget *table = new QTableWidget(this);
table->setItemDelegateForColumn(index_column, delegate);
// or table->setItemDelegateForRow(index_row, delegate);
// or table->setItemDelegate(delegate);
To editor was opened always, you should use openPersistentEditor(). For example:
QTableWidgetItem *item = new QTableWidgetItem;
table->insertRow(row);
table->setItem(row, index_your_delegate, item);
table->openPersistentEditor(item);

Resources