gpt4 book ai didi

c++ - QHeaderView 样式每列

转载 作者:行者123 更新时间:2023-11-30 03:31:19 24 4
gpt4 key购买 nike

我正在尝试编辑 QTreeWidget 标题的样式。

我发现我可以使用 QHeaderView::section 编辑它,然后编辑背景、颜色、边框...

但是,我想单独编辑专门的列标题。我在 documentation 上找到了我们可以使用 ::first, ::last...

有没有办法精确指定另一个部分(例如 [index = 3])?

最佳答案

不,除了使用 ::first::last 之外,没有办法用样式表改变标题部分的外观: :中间QStylesheetStyle(加载样式表时使用的样式表)仅实现这些状态。

要解决它,您基本上可以使用重新实现其 paintEvent 的自定义 QHeaderView,或者我推荐您的选项:使用自定义样式 ( QProxyStyle 是一个不错的选择,因为它允许您仅实现所需的功能并从 base 样式继承其余功能)。您必须专门为 CE_Header 元素重新实现 drawControl 方法:

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

注意:我设法让它只与 fusion 风格一起工作。 windows 样式似乎实现了它自己的标题配色方案。如果你想使用这样的样式,那么你应该手动绘制标题(在相同的 drawControl 中,没有必要重新实现 QHeaderView)。

要使用自定义样式,只需 qApp->setStyle(new MyProxyStyle("fusion"));(将 fusion 作为基本样式)。

结果

header


非常重要的注意事项:您必须知道,as indicated in the documentation ,您不能同时使用自定义QStyle和样式表:

Warning: Qt style sheets are currently not supported for custom QStyle subclasses. We plan to address this in some future release.


上一个回答

我之前错误地回答了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 代替)。例如,其他角色将允许您调整边框和文本颜色。

结果

融合风格:

fusion

使用 windows 风格:

windows

关于c++ - QHeaderView 样式每列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44303603/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com