2017-06-01 109 views
1

我想編輯QTreeWidget的標題樣式。QHeaderView風格每列

我發現我可以用QHeaderView::section還有,編輯背景,顏色,邊框編輯...

不過,我想特別單獨編輯列標題。我在documentation,我們可以使用::first::last發現...

有什麼辦法來精確指定另一部分(與類似[index = 3]例如)?

+0

(「大家好」的方式......看來我不能編輯帖子補充一點:在一些簡單的情況下,可以通過使用化QAbstractItemModel的方法和角色的變化QHeaderView的部分顏色...) – Manu310

回答

2

不,除了使用::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"));(以融合爲基礎樣式)。

結果

header


請格外注意:你必須認識到,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代替)。例如,其他角色將允許您調整邊框和文本顏色。

結果

隨着融合風格:

fusion

隨着窗口風格:

windows

+0

哎呀,我讀了它,不知道爲什麼想到'QTabBar' ...讓我在一分鐘後發佈更改 – cbuchart

+0

更新!對此感到抱歉 – cbuchart

+0

哇!:)它超出了我所知道的範圍,我不知道我是否可以使用它(這是使用C++核心和JS引擎的相當大的應用程序的一部分,所以我不會如果我可以(「允許」)重新定義這個......我們還是很瞭解,再加上我們使用了StyleSheets,而且我們仍然在使用Qt 4(即將移動到5,最後^^)。我實現了這一點,但無論如何感謝細節!我想現在我會使用::第一等等;) – Manu310

0
// 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);