Reputation: 304
How to disable focus border and background it QTreeWidget with fusion style? The focus is wery annoying. Especially when I use alternating raw color.
#include <QApplication>
#include <QWidget>
#include <QTreeWidget>
#include <QMessageBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle("fusion");
QWidget *mainWidget = new QWidget();
mainWidget->resize(200,150);
QTreeWidget *myTree = new QTreeWidget(mainWidget);
myTree->resize(200,150);
QTreeWidgetItem *item;
item = new QTreeWidgetItem(myTree);
item->setText(0,"item1");
//...
item = new QTreeWidgetItem(myTree);
item->setText(0,"item6");
myTree->setAlternatingRowColors(true);
myTree->setStyleSheet("QTreeView {background-color: #222222;"
" alternate-background-color: #333333;"
" selection-background-color: #FF77FF;}");
myTree->setFocus();
item->setSelected(true);
mainWidget->show();
return a.exec();
}
Upvotes: 2
Views: 2439
Reputation: 11
I've modified Aleph0's code it works for Fusion or Windows style.
class CustomStyle : public QProxyStyle
{
public:
CustomStyle(QString style) {
if (style == "Windows") {
m_style = QStyleFactory::create("Windows");
} else if (style == "Fusion") {
m_style = QStyleFactory::create("Fusion");
} else {
m_style = new QProxyStyle();
}
}
void drawPrimitive(PrimitiveElement element, const QStyleOption *option,
QPainter *painter, const QWidget *widget) const
{
// Disables focus drawing for a widget
if (element == QStyle::PE_FrameFocusRect) return;
m_style->drawPrimitive(element, option, painter, widget);
}
private:
QStyle* m_style;
};
auto m_tree_device = new QTreeWidget();
auto m_tree_device->setStyle(new CustomStyle("Windows"));
-- .qss file ----
// Item is selected and focused
QTreeView::item:selected:active {
background-color: #007ACC;
}
// Item is selected and unfocused
QTreeView::item:selected:!active {
background-color: lightgray;
}
Upvotes: 0
Reputation: 6084
I took me some hard time to figure this out, but I also gained some understanding in the Qt Stylesheets. It was basically necessary to set the CSS properties of the QTreeView::item
in case the pseudostate selected
is disabled and the pseudostate focus
is enabled.
If one wants to disable the drawing of the small focus rectangle it is possible to do this using a custom QProxyStyle
as shown in (https://stackoverflow.com/a/17294081/5762796).
Seemingly, there is no Qt Css property for the styling of the focus frame.
#include <QApplication>
#include <QWidget>
#include <QTreeWidget>
#include <QProxyStyle>
// Disables focus drawing for all widgets
class Style_tweaks : public QProxyStyle
{
public:
void drawPrimitive(PrimitiveElement element, const QStyleOption *option,
QPainter *painter, const QWidget *widget) const
{
if (element == QStyle::PE_FrameFocusRect) return;
QProxyStyle::drawPrimitive(element, option, painter, widget);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setStyle("fusion");
a.setStyle(new Style_tweaks);
auto myTree = new QTreeWidget;
myTree->resize(200, 150);
auto item1 = new QTreeWidgetItem(myTree);
item1->setText(0, "item1");
auto item2 = new QTreeWidgetItem(myTree);
item2->setText(0, "item6");
auto item3 = new QTreeWidgetItem(myTree);
item3->setText(0, "item7");
myTree->setAlternatingRowColors(true);
myTree->setStyleSheet("\
QTreeView {background-color: #222222; alternate-background-color: #333333; selection-background-color: #FF77FF; } \
QTreeView::item:!selected:focus { background-color: #222222; alternate-background-color: #333333; selection-background-color: #222222;outline-color: white}\
");
/* item3->setSelected(true);*/
myTree->setFocus();
item3->setSelected(true);
myTree->show();
return a.exec();
}
Upvotes: 3