How to change highlight color on QTreewidgetItem - qt

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

Related

QLabel change font color without changing any other style

I want to change the color of the text in a QLabel dynamically.
I have defined the color and style of the QLabel in the ui file and I want to change it when a certain event takes place.
I want to change the color without changing any other style of my QLabel.
I have found several answers adressing the issue of changing text color in a QLabel (1, 2, 3) and they all use the function setStyleSheet. This function works fine but it changes my font size and other styles related to the QLabel.
I have seen that the problem is related to setStyleSheet ignoring any previous style. The solution proposed there involves retrieving all the styles I want to maintain and setting them again together with the text color change.
This is cumbersome and difficult to maintain. If more styles were defined in the future I would need to review this part of the code to be able to reset all of them.
I would like to be able to change QLabel text color without altering any other syle. Is it possible?
If you want to manage the text color of QLabel you could wrap it with customized class.
For example:
class ColorLabel : public QLabel
{
public:
ColorLabel(const QString &text, QWidget *parent = nullptr)
: QLabel(text, parent)
{
setAutoFillBackground(true);
}
void setTextColor(const QColor &color)
{
QPalette palette = this->palette();
palette.setColor(this->backgroundRole(), color);
palette.setColor(this->foregroundRole(), color);
this->setPalette(palette);
}
};
And to use it in your code:
ColorLabel * poColorLabel = new ColorLabel("My string", this);
poColorLabel->setTextColor(Qt::red); // set label text in red color
FYI: I tested it on Fedora, Qt5.12 and it works fine.
A pragmatic approach:
Utilize the cascadingness of CSS.
Wrap your QLabel in a QWidget (don't forget a QLayout).
Set your default style on the surrounding QWidget.
Set the font color as the QLabel's only style.
You can create some style class to control a widget's style:
class WidgetStyleSheet
{
public:
// change some style's value
void setValue(const QString& styleKey, const QString& value)
{
_styleMap[styleKey] = value;
}
// to default state
void reset() {}
// form stylesheet
QString toStyleSheet() const
{
QString styleSheet;
QMapIterator<QString, QString> iter(_styleMap);
while( iter.hasNext() )
styleSheet += QString("%1: %2").arg(iter.key()).arg(iter.value());
return styleSheet;
}
private:
QMap<QString, QString> _styleMap;
}
Somewhere in your code:
WidgetStyleSheet labelSS;
// ...
labelSS.setValue("color", QString("%1").arg( QColor(255, 10, 0).name() );
labelSS.setValue("background-color", "...");
// ...
label->setStyleSheet(labelSS);
The following works fine. But it is not that elegant. This is in python. You have to pass the button name (or any other) to the following as is defined in the array
btns = ['self.hBeamBtn','self.lBeamBtn','self.allTestBtn','self.prnStatusBtn']
for btn in btns:
if str(btn_name) == str(btn):
styl = btn+'.setStyleSheet("font: bold;background-color: red;font-size: 12px;height: 28px;width: 80px;")'
eval(styl)

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

Change color of purple tab text in Konsole CSS

When input comes in on a tab that is not active, the text for the tab changes to a purple color. What CSS selectors do I need to use to change this?
I am using a custom stylesheet in Konsole to change how the tabs look, but can't figure out how to change this one value. This page makes no mention of it.
I'm using Konsole 2.13.2(KDE 4.13.3) on Xubuntu 14.04(XFCE).
As of today, this tab-activity color appears to be set by
void TabbedViewContainer::setTabActivity(int index , bool activity)
{
const QPalette& palette = _tabBar->palette();
KColorScheme colorScheme(palette.currentColorGroup());
const QColor colorSchemeActive = colorScheme.foreground(KColorScheme::ActiveText).color();
const QColor normalColor = palette.text().color();
const QColor activityColor = KColorUtils::mix(normalColor, colorSchemeActive);
QColor color = activity ? activityColor : QColor();
if (color != _tabBar->tabTextColor(index))
_tabBar->setTabTextColor(index, color);
}
in konsole's src/ViewContainer.cpp and is therefore probably beyond the reach of a custom stylesheet configured in Konsole.
Note how KColorScheme::ActiveText is mixed with normalColor. You can have some influence over the color by changing the "Active Text" color in KDE System Settings -> Color -> Colors tab -> Active Text. Konsole has to restarted for the changes to take effect.

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

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