我正在尝试编辑QTreeWidget标题的样式。
我发现我可以用QHeaderView::section编辑它,编辑背景,颜色,边框.
但是,我想单独编辑特定的列标题。我在文档上发现我们可以用::first,::last.
是否有任何方法可以精确地指定另一节(例如,使用类似[index = 3]的内容)?
发布于 2017-06-01 12:48:58
不,除了使用::first、::last、::middle之外,没有办法用样式表更改标题部分的外观。QStylesheetStyle (在加载样式表时使用的)只实现这些状态。
要解决这个问题,您基本上可以使用重新实现其paintEvent的自定义paintEvent,也可以使用我推荐的选项:使用自定义样式( QProxyStyle是一个很好的选项,因为它只允许您实现所需的功能,并从基本样式继承其余功能)。您必须为drawControl元素重新实现CE_Header方法:
virtual void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const override {
if (element == CE_Header) { // sections
// ...
} else { // default behaviour for anything else
QProxyStyle::drawControl(element, option, painter, widget);
}
}现在,QStyleOptionHeader::section变量包含正在绘制的部分的索引,因此您可以使用它来计算颜色。
最低代理样式的完整代码将是:
class MyProxyStyle : public QProxyStyle {
public:
MyProxyStyle(const QString& name) : // "fusion", "windows", ...
QProxyStyle(name) {
}
virtual void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const override {
if (element == CE_Header) {
auto ho = *qstyleoption_cast<const QStyleOptionHeader*>(option);
auto headerView = qobject_cast<const QHeaderView*>(widget);
ho.text = QString::number(ho.section); // for testing, it prints the section index
auto pal = ho.palette;
const QColor color(ho.section * 32 + 64, 0, 0); // color based on index
pal.setBrush(QPalette::All, QPalette::Button, color);
ho.palette = pal;
QProxyStyle::drawControl(element, &ho, painter, widget);
} else {
QProxyStyle::drawControl(element, option, painter, widget);
}
}
};注意:我已经设法使它只与融合风格一起工作。windows样式似乎实现了自己的页眉颜色方案。如果您想使用这种样式,那么您应该手动绘制标题(在同一个drawControl中,没有必要重新实现QHeaderView)。
要使用自定义样式,只需使用qApp->setStyle(new MyProxyStyle("fusion")); (将融合作为基本样式)。
结果

非常重要的注意事项:您必须知道,如文件所示,还不能同时使用自定义QStyle和样式表:
警告:自定义QStyle子类目前不支持Qt样式表。我们计划在将来的发行版中解决这个问题。
先前的答案
我以前错误地回答了QTabBar问题的问题,这个问题恰好非常相似:除了一些预定义的选项卡(例如,第一个或最后一个)之外,不可能使用样式表来配置给定的选项卡。我们必须要么重新实现QTabBar,要么使用自定义样式(如前所述)。我会保留它的解决方案,以防它对别人有用。
棘手的部分是样式选项没有任何关于选项卡索引的信息,所以您必须以某种方式解决它。我发现使用选项卡的x位置(从选项和QTabBar都可以访问)是匹配选项卡的有效指示器。如果您的选项卡条是垂直呈现的,则应该使用y坐标,如果选项卡条是多行的,则使用整个rect。
最低代理样式的完整代码将是:
class MyProxyStyle : public QProxyStyle {
public:
MyProxyStyle(const QString& name) : // "fusion", "windows", ...
QProxyStyle(name) {
}
virtual void drawControl(ControlElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget) const override {
if (element == CE_TabBarTab) {
auto to = *qstyleoption_cast<const QStyleOptionTab*>(option);
auto tabBar = qobject_cast<const QTabBar*>(widget);
for (int ii = 0; ii < tabBar->count(); ++ii) { // must find manually the tab
const auto rect = tabBar->tabRect(ii);
if (rect.x() == to.rect.x()) { // found the index of tab being painted
to.text = QString::number(ii); // for testing, it prints the tab index
auto pal = to.palette;
const QColor color(ii * 32 + 64, 0, 0); // color based on index
pal.setBrush(QPalette::All, QPalette::Button, color);
pal.setBrush(QPalette::All, QPalette::Background, color);
to.palette = pal;
break;
}
}
QProxyStyle::drawControl(element, &to, painter, widget);
} else {
QProxyStyle::drawControl(element, option, painter, widget);
}
}
};在设置画笔时使用不同颜色角色的原因是,不同的样式在绘制部分时使用不同的角色(融合样式使用QPalette::Button作为背景,而windows则使用QPalette::Background )。例如,其他角色将允许您调整边框和文本颜色。
结果
具有融合风格:

窗口样式:

发布于 2017-10-12 18:25:14
在一些简单的情况下,您可以通过使用QHeaderView和角色的方法来更改QAbstractItemModel的区段颜色:
// text of 0-section will be red in header
m_model.setHeaderData(0, Qt::Horizontal, QBrush(Qt::red), Qt::ForegroundRole);
// background of a 25-section will be blue in header
m_model.setHeaderData(25, Qt::Horizontal, QBrush(Qt::blue), Qt::BackgroundRole);https://stackoverflow.com/questions/44303603
复制相似问题