Qt的QTableWidget样式设置

在 Qt 中,可以通过样式表(QSS)为 QTableWidget 设置各种样式。以下是一些常见的样式设置示例:

1. 基本样式设置

cpp 复制代码
tableWidget->setStyleSheet(
    // 表格整体样式
    "QTableWidget {"
    "   background-color: #F0F0F0;"  // 背景色
    "   gridline-color: #C0C0C0;"     // 网格线颜色
    "   border: 1px solid gray;"     // 边框
    "}"
    
    // 表头样式
    "QHeaderView::section {"
    "   background-color: #404040;"  // 表头背景
    "   color: white;"               // 文字颜色
    "   padding: 4px;"               // 内边距
    "   border: 1px solid #505050;"  // 边框
    "   min-height: 25px;"           // 最小高度
    "}"
    
    // 单元格样式
    "QTableWidget::item {"
    "   color: #333333;"            // 文字颜色
    "   border-bottom: 1px solid #D0D0D0;"  // 底部边框
    "}"
    
    // 选中状态
    "QTableWidget::item:selected {"
    "   background-color: #B8D6FF;"  // 选中背景色
    "   color: black;"               // 选中文字颜色
    "}"
);

2. 高级样式设置

cpp 复制代码
// 交替行颜色(需要开启交替行颜色功能)
tableWidget->setAlternatingRowColors(true);
tableWidget->setStyleSheet(
    "QTableWidget { alternate-background-color: #F8F8F8; }"
);

// 角部按钮样式(表格左上角按钮)
tableWidget->setStyleSheet(
    "QTableCornerButton::section {"
    "   background-color: #404040;"
    "   border: 1px solid #505050;"
    "}"
);

// 禁用状态样式
tableWidget->setStyleSheet(
    "QTableWidget:disabled {"
    "   color: #808080;"
    "   background-color: #F0F0F0;"
    "}"
);

// 设置特定列宽/行高
tableWidget->horizontalHeader()->setDefaultSectionSize(150);  // 列宽
tableWidget->verticalHeader()->setDefaultSectionSize(30);     // 行高

3. 自定义单元格样式

cpp 复制代码
// 通过代理自定义样式(需要继承 QStyledItemDelegate)
class CustomDelegate : public QStyledItemDelegate {
public:
    void paint(QPainter* painter, const QStyleOptionViewItem& option, 
              const QModelIndex& index) const override {
        // 自定义绘制逻辑
        if(index.column() == 0) {
            painter->fillRect(option.rect, QColor("#FFE4E1"));
        }
        QStyledItemDelegate::paint(painter, option, index);
    }
};

// 设置代理
tableWidget->setItemDelegate(new CustomDelegate());

4. 动态样式设置

cpp 复制代码
// 修改特定行颜色
for(int row = 0; row < tableWidget->rowCount(); ++row) {
    QTableWidgetItem* item = tableWidget->item(row, 0);
    if(item) {
        item->setBackground(QColor("#E6F3FF"));
        item->setForeground(Qt::blue);
    }
}

// 设置特定单元格样式
QTableWidgetItem* specialItem = new QTableWidgetItem("Important");
specialItem->setData(Qt::UserRole, "special");
tableWidget->setItem(0, 0, specialItem);

// 在样式表中添加特殊样式
tableWidget->setStyleSheet(
    "QTableWidgetItem[special=\"true\"] {"
    "   background-color: #FFD700;"
    "   font-weight: bold;"
    "}"
);

5. 完整样式表示例

cpp 复制代码
tableWidget->setStyleSheet(R"(
    QTableWidget {
        background-color: #FFFFFF;
        alternate-background-color: #F8F8F8;
        gridline-color: #E0E0E0;
        border: 1px solid #D0D0D0;
        font-size: 12px;
    }

    QHeaderView::section {
        background-color: #0078D4;
        color: white;
        padding: 4px;
        border: none;
        min-height: 28px;
    }

    QHeaderView::section:hover {
        background-color: #006CBC;
    }

    QHeaderView::section:pressed {
        background-color: #005AA3;
    }

    QTableWidget::item {
        border-bottom: 1px solid #E0E0E0;
        padding: 4px;
    }

    QTableWidget::item:selected {
        background-color: #B8D6FF;
        color: #000000;
    }

    QTableCornerButton::section {
        background-color: #0078D4;
        border: none;
    }

    QScrollBar:vertical {
        width: 12px;
        background: #F0F0F0;
    }

    QScrollBar::handle:vertical {
        background: #C0C0C0;
        min-height: 20px;
    }
)");

注意事项:

  1. 样式表优先级高于代码设置的属性
  2. 使用 alternate-background-color 需要先调用 setAlternatingRowColors(true)
  3. 复杂的样式建议使用 QSS 文件管理
  4. 对于性能敏感的场景,避免频繁修改样式表
  5. 可以使用 Qt Style Sheet Reference 查看所有可用属性
相关推荐
大猫会长1 分钟前
mac中创建 .command 文件,执行node服务
前端·chrome
旧时光_1 分钟前
Zustand 状态管理库完全指南 - 进阶篇
前端·react.js
snakeshe10103 分钟前
深入理解useState:批量更新与非函数参数支持
前端
windliang3 分钟前
Cursor 排查 eslint 问题全过程记录
前端·cursor
boleixiongdi4 分钟前
# Bsin-App Uni:面向未来的跨端开发框架深度解析
前端
G等你下课7 分钟前
AJAX请求跨域问题
前端·javascript·http
前端西瓜哥8 分钟前
pixijs 的填充渲染错误,如何处理?
前端
snakeshe10108 分钟前
6-1. 实现 useState
前端
呆呆没有脑袋11 分钟前
深入浅出 JavaScript 闭包:从核心概念到框架实践
前端
snakeshe101012 分钟前
用100行代码实现React useState钩子:多状态管理揭秘
前端