QTableView設置QSS樣式表
color: white; /*表格內文字顏色*/
gridline-color: black; /*表格內框顏色*/
background-color: rgb(108, 108, 108); /*表格內背景色*/
alternate-background-color: rgb(64, 64, 64);
selection-color: white; /*選中區域的文字顏色*/
selection-background-color: rgb(77, 77, 77); /*選中區域的背景色*/
border: 2px groove gray;
border-radius: 0px;
padding: 2px 4px;
只設置此行的這一列的背景顏色,設置成功。
如:item->setBackgroundColor(QColor(0,60,10));//也可以使用qt系統的默認顏色。
設置單元格字體顏色、背景顏色和字體字符:
QTableWidgetItem *item = new QTableWidgetItem("Apple");
item->setBackgroundColor(QColor(0,60,10));
item->setTextColor(QColor(200,111,100));
item->setFont(QFont("Helvetica"));
tableWidget->setItem(0,3,item);
/* 表格設置 */
QTableView{ /** QTableView設置 */
border: none;
background-color: white;
selection-background-color: black;
}
QHeaderView::section{ /*表頭設置*/
border: none;
height:20px; /*表頭高度*/
color: white;
font-size: 15px;
font-weight: 900;
background-color: rgb(80, 80, 80);
text-align: center;
}
QTableView::item{ /** 每個單元格設置 */
background-color: white;
border: none;
background-color: rgb(220, 220, 220); /*交替行,第二行*/
selection-background-color: white;
selection-color: black;
font-family: Consolas;
font-size: 11px;
text-align: center;
}
QTableView::item:!alternate:!selected{
background-color: white; /*交替行的另一顏色*/
selection-background-color: rgb(220, 220, 220);
selection-color: black;
}
# 如果要顯示兩種顏色還要在代碼中添加
ui->tableView_name->setShowGrid(false); //<-----不顯示grid
ui->tableView_name->setAlternatingRowColors(true); //<-----雙色顯示