设置tableview选中行的背景色

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

    //设置为透明色

    [self.lookOverTableViewcellForRowAtIndexPath:indexPath].selectionStyle

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Qt 的 `QTableView` 控件中,我们可以通过 `QAbstractItemView::itemDelegate()` 函数获取到当前控件的委托(Delegate)。而在委托类中,我们可以重载 `QStyledItemDelegate::paint()` 函数来自定义绘制每个表格单元格的外观。 在 `paint()` 函数中,我们可以通过 `QStyleOptionViewItem::palette` 属性来获取当前表格单元格的调色板(Palette),即背景色、前景色等颜色信息。具体代码如下: ```cpp void MyItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyledItemDelegate::paint(painter, option, index); // 获取当前单元格的背景色 QPalette::ColorRole role = QPalette::Base; if (option.state & QStyle::State_Selected) role = QPalette::Highlight; QColor bgColor = option.palette.color(option.palette.currentColorGroup(), role); // ...绘制其他内容 } ``` 上述代码中,我们首先调用了 `QStyledItemDelegate::paint()` 函数,以保留默认的单元格绘制。然后通过 `option.palette` 属性获取到了当前单元格的调色板,并根据选中状态设置 `role` 变量。最后调用 `QPalette::color()` 函数获取到了当前单元格的背景色 `bgColor`,可以用于绘制其他内容。 需要注意的是,如果我们想要获取表格控件中所有单元格的背景色,可以通过遍历模型中的数据来获取。具体实现方法可以参考下面的代码: ```cpp QAbstractItemModel *model = tableView->model(); for (int row = 0; row < model->rowCount(); row++) { for (int col = 0; col < model->columnCount(); col++) { QModelIndex index = model->index(row, col); QStyleOptionViewItem option = tableView->viewOptions(); option.rect = tableView->visualRect(index); // 获取当前单元格的背景色 QPalette::ColorRole role = QPalette::Base; if (tableView->selectionModel()->isSelected(index)) role = QPalette::Highlight; QColor bgColor = option.palette.color(option.palette.currentColorGroup(), role); // ...处理背景色 } } ``` 在上述代码中,我们通过遍历模型中的数据来获取每个单元格的索引,并通过 `tableView->visualRect(index)` 函数获取到单元格的位置和大小。然后调用 `option.palette` 函数获取到单元格的调色板,并根据选中状态设置 `role` 变量,最终获取到了每个单元格的背景色

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值