Home > Mobile >  Unable to set tablewidget header background color
Unable to set tablewidget header background color

Time:12-02

The following program, in addition to setting the header background color of the item (backgroud) doesn't work, other everything is normal,
Also tried to use setBackgroundColor (), but both the header of the background color doesn't work,

Strange is the header item foreground setForeground is no problem, but also for general list items all OK!

How to solve the item set header background color?

Thank you very much!



#include

#include
#include

#include

Int main (int arg c, char * argv [])
{
QApplication a (arg c, argv);

QTableWidget * table=new QTableWidget;

Table - & gt; SetColumnCount (4);
Table - & gt; SetRowCount (2);


The QStringList header;
Header<" 1 "& lt; <" 2 "& lt; <" 3 "& lt; <" 4 ";
Table - & gt; SetHorizontalHeaderLabels (header);

for(int i=0; i<4. I++)
{

QString STRTMP;

QTableWidgetItem * columnHeaderItem=table - & gt; HorizontalHeaderItem (I);
ColumnHeaderItem - & gt; SetBackground (QBrush (QColor (0255, 0)));//set the background color of header item doesn't work, why???????
ColumnHeaderItem - & gt; ,0,255 setForeground (QBrush (QColor (0)));///////set the header item foreground OK!
ColumnHeaderItem - & gt; SetText (STRTMP setNum (I));

Table - & gt; , columnHeaderItem setHorizontalHeaderItem (I);


QTableWidgetItem * columnHeaderItem1=new QTableWidgetItem;
ColumnHeaderItem1 - & gt; SetBackground (QBrush (QColor (0255, 0)));/////set the background color of table item OK!
ColumnHeaderItem1 - & gt; SetForeground (QBrush (QColor (0, 255)));
ColumnHeaderItem1 - & gt; SetText (STRTMP setNum (I + 4));

Table - & gt; SetItem (0, I, columnHeaderItem1);


QTableWidgetItem * columnHeaderItem2=new QTableWidgetItem;
ColumnHeaderItem2 - & gt; SetBackground (QBrush (QColor (255255, 0)));///////////////set the background color of table item OK!
ColumnHeaderItem2 - & gt; ,0,255 setForeground (QBrush (QColor (0)));
ColumnHeaderItem2 - & gt; SetText (STRTMP setNum + 8) (I);

Table - & gt; SetItem (1, I, columnHeaderItem2);

}

Table - & gt; Show ();

Return a.e xec ();
}

CodePudding user response:

Please look at the great god, which exactly is what reason,

thank you

CodePudding user response:

Try the stylesheet, baidu stylesheet

CodePudding user response:

The UI - & gt; TableWidget - & gt; The horizontalHeader () - & gt; SetStyleSheet QHeaderView: : section (" {background: skyblue; } ");

CodePudding user response:

Stylesheet is stronger than using direct call function effectively

CodePudding user response:

With o, stylesheet can't set the specified header,

CodePudding user response:

Self. TableWidget. HorizontalHeader (.) setStyleSheet QHeaderView: : section (" {background: skyblue;
\ n""12 pt font: \ line " "; #} ") set the header background color and font color
  •  Tags:  
  • Qt
  • Related