关于tableView的section悬浮

UITableViewController的section在UITableViewStylePlain下滑动时会悬浮的,而在UITableViewStyleGrouped下是会随着cell一起滑动的.所以如果你想让section随着cell一起滑动的效果,那么在创建UITableViewController时就把style设置成UITableViewStyleGrouped


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现鼠标悬浮选中一行,可以通过继承 QStyledItemDelegate 类来实现自定义的 item delegate。以下是一个简单的实现方式: 1. 定义一个新的 item delegate 类,继承自 QStyledItemDelegate: ```cpp class HoverHighlightDelegate : public QStyledItemDelegate { public: HoverHighlightDelegate(QObject* parent = nullptr); void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override; bool editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) override; private: QModelIndex m_hoveredIndex; }; ``` 2. 重写 paint() 函数,在绘制每个 item 时根据当前鼠标悬浮的项来设置选中状态: ```cpp void HoverHighlightDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const { QStyleOptionViewItem newOption(option); // 如果当前项被鼠标悬浮,则设置为选中状态 if (index == m_hoveredIndex) { newOption.state |= QStyle::State_Selected; } QStyledItemDelegate::paint(painter, newOption, index); } ``` 3. 重写 editorEvent() 函数,监听鼠标事件并保存当前鼠标悬浮的项: ```cpp bool HoverHighlightDelegate::editorEvent(QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option, const QModelIndex& index) { switch (event->type()) { case QEvent::MouseMove: m_hoveredIndex = index; // 记录当前鼠标悬浮的项 break; case QEvent::Leave: m_hoveredIndex = QModelIndex(); // 鼠标离开时清除记录 break; } return QStyledItemDelegate::editorEvent(event, model, option, index); } ``` 4. 在使用该 item delegate 的 table view 中设置该 delegate: ```cpp HoverHighlightDelegate* delegate = new HoverHighlightDelegate(tableView); tableView->setItemDelegate(delegate); ``` 这样就能实现在鼠标悬浮时选中一行的效果了。如果需要自定义选中时的样式,可以在 paint() 函数中进一步修改 QStyleOptionViewItem 的属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值