QTableView设置样式表/选中行颜色, QTableView美化

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); //<-----双色显示
posted @ 2022-02-20 23:46  看不见的R  阅读(11808)  评论(0编辑  收藏  举报