2012-07-12 23 views
5

के शीर्षलेख पृष्ठभूमि रंग को कैसे बदलें, मैंने वर्तमान में कोशिश की है। हेडर टेक्स्ट सही ढंग से रंग बदलता है लेकिन पृष्ठभूमि डिफ़ॉल्ट से नहीं बदलेगी।QTableView

template<typename T> 
inline QVariant TableModel<T>::headerData(int section, Qt::Orientation orientation, int role) const 
{ 
    //... 
    else if(role == Qt::BackgroundRole) { 
     return QBrush(m_display.headerBackground); 
    } 
    //... 
} 

मैं पृष्ठभूमि रंग कैसे सेट कर सकता हूं?

+0

क्या यह मान स्थिर है - क्या यह फ़ंक्शन प्रत्येक बार इस मॉडल को किसी मॉडल के उदाहरण पर कभी भी बुलाया जाता है? यदि नहीं, तो क्या आप इस दृश्य को सूचित करने के लिए प्रासंगिक सिग्नल उत्सर्जित कर रहे हैं कि हेडर डेटा बदल गया है? –

उत्तर

4

यहाँ एक वैकल्पिक समाधान है सकते हैं।

MyTableView::MyTableView(QWidget* parent) : QTableView(parent) 
{ 
    ... 
    // Make a copy of the current header palette. 
    QPalette palette = horizontalHeader()->palette(); 

    // Set the normal/active, background color 
    // QPalette::Background is obsolete, use QPalette::Window 
    palette.setColor(QPalette::Normal, QPalette::Window, Qt::red); 

    // Set the palette on the header. 
    horizontalHeader()->setPalette(palette); 
} 
+0

यह समाधान Qt 5.9.1 का उपयोग करके मेरे लिए काम नहीं करता है, लेकिन स्टाइलशीट समाधान करता है! – ForeverLearning