我想編輯QTreeWidget
的標題樣式。QHeaderView風格每列
我發現我可以用QHeaderView::section
還有,編輯背景,顏色,邊框編輯...
不過,我想特別單獨編輯列標題。我在documentation,我們可以使用::first
,::last
發現...
有什麼辦法來精確指定另一部分(與類似[index = 3]
例如)?
我想編輯QTreeWidget
的標題樣式。QHeaderView風格每列
我發現我可以用QHeaderView::section
還有,編輯背景,顏色,邊框編輯...
不過,我想特別單獨編輯列標題。我在documentation,我們可以使用::first
,::last
發現...
有什麼辦法來精確指定另一部分(與類似[index = 3]
例如)?
不,除了使用::first
,::last
,::middle
以外,沒有辦法改變標題部分的外觀。 QStylesheetStyle
(樣式表加載時使用的那個)只能實現這些狀態。
爲了解決這個問題,你可以基本上要麼使用自定義QHeaderView
是重新實現其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);
}
}
};
注:我已經成功地得到它與融合僅風格的工作。看起來,窗口樣式實現了它自己的標題顏色方案。如果你想使用這種風格,那麼你應該手動畫頭(在同一drawControl
,沒有必要重新實施QHeaderView
)。
使用自定義樣式只需qApp->setStyle(new MyProxyStyle("fusion"));
(以融合爲基礎樣式)。
結果
請格外注意:你必須認識到,as indicated in the documentation,你不能使用自定義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
爲背景,而窗口使用QPalette::Background
代替)。例如,其他角色將允許您調整邊框和文本顏色。
結果
隨着融合風格:
隨着窗口風格:
// 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);
(「大家好」的方式......看來我不能編輯帖子補充一點:在一些簡單的情況下,可以通過使用化QAbstractItemModel的方法和角色的變化QHeaderView的部分顏色...) – Manu310