2025-03-14 16:06:20 +08:00
|
|
|
|
#include "tableWidgetHoverDelegate.h"
|
2025-03-27 14:47:51 +08:00
|
|
|
|
#include <QPainter>
|
2025-03-14 16:06:20 +08:00
|
|
|
|
|
|
|
|
|
|
QTableWidgetHoverDelegate::QTableWidgetHoverDelegate(QTableWidget *parent)
|
|
|
|
|
|
: QStyledItemDelegate{parent}
|
|
|
|
|
|
{
|
|
|
|
|
|
m_tableWiget = parent;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QTableWidgetHoverDelegate::~QTableWidgetHoverDelegate()
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
void QTableWidgetHoverDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
|
|
|
|
|
{
|
2025-03-24 18:13:06 +08:00
|
|
|
|
if(option.state.testFlag(QStyle::State_MouseOver) && m_tableWiget)
|
2025-03-14 16:06:20 +08:00
|
|
|
|
{
|
|
|
|
|
|
QTableWidgetItem* hoveredItem = m_tableWiget->item(index.row(), index.column());
|
|
|
|
|
|
if(hoveredItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
int row = index.row();
|
|
|
|
|
|
for(int column = 0; column < m_tableWiget->columnCount(); column++)
|
|
|
|
|
|
{
|
|
|
|
|
|
QTableWidgetItem* item = m_tableWiget->item(row, column);
|
|
|
|
|
|
if(!item)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
QModelIndex index = m_tableWiget->model()->index(row, column);
|
|
|
|
|
|
QStyleOptionViewItem itemOption = option;
|
|
|
|
|
|
itemOption.rect = m_tableWiget->visualItemRect(item);
|
|
|
|
|
|
QStyledItemDelegate::paint(painter, itemOption, index);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2025-03-27 14:47:51 +08:00
|
|
|
|
{
|
|
|
|
|
|
/*int row = index.row();
|
|
|
|
|
|
int col = index.column();
|
|
|
|
|
|
QTableWidgetItem* item = m_tableWiget->item(row, col);
|
|
|
|
|
|
QRect rect = m_tableWiget->visualItemRect(item);
|
|
|
|
|
|
qDebug() << row << ", " << col << " optionRect:" << option.rect << " visualItemRect:" << rect;
|
|
|
|
|
|
painter->save();
|
|
|
|
|
|
painter->setPen(Qt::red);
|
|
|
|
|
|
painter->drawLine(rect.x(), rect.y() + rect.height()*0.5, rect.x() + rect.width(), rect.y() + rect.height()*0.5); //painter属于view,所以要以view的坐标系为标准
|
|
|
|
|
|
painter->restore();*/
|
2025-03-14 16:06:20 +08:00
|
|
|
|
QStyledItemDelegate::paint(painter, option, index);
|
2025-03-27 14:47:51 +08:00
|
|
|
|
}
|
2025-03-14 16:06:20 +08:00
|
|
|
|
}
|