如何禁用焦点边框和QTreeWidget具有融合样式的背景?焦点是烦人的烦人。特别是当我使用交替的原始颜色时。
#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();
}
最佳答案
我花了一些时间来解决这个问题,但是我在Qt样式表中也获得了一些理解。在禁用伪状态QTreeView::item
和启用伪状态selected
的情况下,基本上有必要设置focus
的CSS属性。
如果要禁用小焦点矩形的绘制,则可以使用(https://stackoverflow.com/a/17294081/5762796)中所示的自定义QProxyStyle
进行此操作。
看来,没有焦点框架样式的Qt Css属性。
#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();
}
关于windows - 如何禁用焦点边框和QTreeWidget具有融合样式的背景?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55847961/