自己写了一个函数,封装QTableWidget的样式,根据自己的需求进行修改单元格的文字颜色、背景颜色等。
- I wrote a function to encapsulate the style of QTableWidget and modify the text color and background color of the cell according to my own requirements.
/****************************************************************************
** file:
** brief: QTableWidgetItem设置样式
** Copyright (C)
** Author:
** E-Mail:
** Version 2.0.1
** Last modified: 2020.07.22
** Modified By: LLL-NEO-Karl
****************************************************************************/
void MainWindow::OnTableWidgetStyleSheet(QTableWidget *table,int row,int col,QString stringdata,QColor TextColor,QColor BackgroundColor,bool setItalic,bool setBold)
{
QTableWidgetItem *item = new QTableWidgetItem(stringdata);//为单元格的内容
item->setTextColor(TextColor);
QFont nullFont;
nullFont.setItalic(setItalic);
nullFont.setBold(setBold);
item->setFont(nullFont);
item->setBackgroundColor(BackgroundColor);
table->setItem(row, col, item);
}
调用:
#if 1//设置显示字体的颜色 和单元格的背景颜色
OnTableWidgetStyleSheet(ui->tableWidget,i,6,"NULL",QColor(255,0,0),QColor(255,255,255),false,true);
OnTableWidgetStyleSheet(ui->tableWidget,i,7,"NULL",QColor(255,0,0),QColor(255,255,255),false,true);
#endif
result: