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

本文详细介绍了如何使用QSS样式表对QTableView进行美化,包括去除边框、隐藏网格线、设置行选中颜色、滚动条样式定制等,以及表格编辑权限、选择方式、字体颜色和单元格背景的调整,最后提供了完整的QSS代码示例。
摘要由CSDN通过智能技术生成

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->setAlternatingRowColor
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值