网站起域名原则,智能建造论文,易店无忧官网,哪家公司建别墅好在 Qt 中#xff0c;可以通过样式表#xff08;QSS#xff09;为 QTableWidget 设置各种样式。以下是一些常见的样式设置示例#xff1a;
1. 基本样式设置
tableWidget-setStyleSheet(// 表格整体样式QTableWidget { background-color: #F0F0F0;…在 Qt 中可以通过样式表QSS为 QTableWidget 设置各种样式。以下是一些常见的样式设置示例
1. 基本样式设置
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. 高级样式设置
// 交替行颜色需要开启交替行颜色功能
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. 自定义单元格样式
// 通过代理自定义样式需要继承 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. 动态样式设置
// 修改特定行颜色
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. 完整样式表示例
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;}
));注意事项
样式表优先级高于代码设置的属性使用 alternate-background-color 需要先调用 setAlternatingRowColors(true)复杂的样式建议使用 QSS 文件管理对于性能敏感的场景避免频繁修改样式表可以使用 Qt Style Sheet Reference 查看所有可用属性