((*iter).curRow == row && (*iter).curCol == column)
{
return *iter;
}
}
return m_InvalidCellSpan;
}
setSpan接口存储了哪些列或者行被合并了,在绘制表格头时我们也可以根据这些信息来计算绘制的大小和位置。
2、表格
数据源介绍完,接下来看看表头视图类,这个类相对Model来说会复杂很多,主要也是根据Model中存储的合并信息来进行绘制。
由于这个类代码比价多,这里我就不贴头文件声明了,下面还是主要介绍下关键函数
a、paintEvent绘制函数
void HHeaderView::paintEvent(QPaintEvent * event)
{
QPainter painter(viewport());
QMultiMap<int, int> rowSpanList;
int cnt = count();
int curRow, curCol;
HHeaderModel * model = qobject_cast<HHeaderModel *>(this->model());
for (int row = 0; row < model->rowCount(QModelIndex()); ++row)
{
for (int col = 0; col < model->columnCount(QModelIndex()); ++col)
{
curRow = row;
curCol = col;
QStyleOptionViewItemV4 opt = viewOptions();
QStyleOptionHeader header_opt;
initStyleOption(&header_opt);
header_opt.textAlignment = Qt::AlignCenter;
// header_opt.icon = QIcon("./Resources/logo.ico");
QFont fnt;
fnt.setBold(true);
header_opt.fontMetrics = QFontMetrics(fnt);
opt.fontMetrics = QFontMetrics(fnt);
QSize size = style()->sizeFromContents(QStyle::CT_HeaderSection, &header_opt, QSize(), this);
// size.setHeight(25);
header_opt.position = QStyleOptionHeader::Middle;
//判断当前行是否处于鼠标悬停状态
if (m_hoverIndex == model->index(row, col, QModelIndex()))
{
header_opt.state |= QStyle::State_MouseOver;
// header_opt.state |= QStyle::State_Active;
}
opt.text = model->item(row, col);
header_opt.text = model->item(row, col);
CellSpan span = model->getSpan(row, col);
int rowSpan = span.rowSpan;
int columnSpan = span.colSpan;
if (columnSpan > 1 && rowSpan > 1)
{
//单元格跨越多列和多行, 不支持,改为多行1列
continue;
/*header_opt.rect = QRect(sectionViewportPosition(logicalIndex(col)), row * size.height(), sectionSizes(col, columnSpan), rowSpan * size.height());
opt.rect = header_opt.rect;
col += columnSpan - 1; */
}
else if (columnSpan > 1)//单元格跨越多列
{
header_opt.rect = QRect(sectionViewportPosition(logicalIndex(col)), row * size.height(), sectionSizes(col, columnSpan), size.height());
opt.rect = header_opt.rect;
col += columnSpan - 1;
}
else if (rowSpan > 1)//单元格跨越多行
{
header_opt.rect = QRect(sectionViewportPosition(logicalIndex(col)), row * size.height(), sectionSize(logicalIndex(col)), size.height() * rowSpan);
opt.rect = header_opt.rect;
for (int i = row + 1; i <= rowSpan - 1; ++i)
{
rowSpanList.insert(i, col);
}
}
else
{
//正常的单元格
header_opt.rect = QRect(sectionViewportPosition(logicalIndex(col)), row * size.height(), sectionSize(logicalIndex(col)), size.height());
opt.rect = header_opt.rect;
}
opt.state = header_opt.state;
//opt.displayAlignment = Qt::AlignCenter;
//opt.icon = QIcon("./Resources/logo.ico");
//opt.backgroundBrush = QBrush(QColor(255, 0, 0));
QMultiMap<int, int>::iterator it = rowSpanList.find(curRow, curCol);
if (it == rowSpanList.end())
{
//保存当前item的矩形
m_itemRectMap[curRow][curCol] = header_opt.rect;
itemDelegate()->paint(&painter, opt, model->index(curRow, curCol, QModelIndex()));
painter.setPen(QColor("#e5e5e5"));
painter.drawLine(opt.rect.bottomLeft(), opt.rect.bottomRight());
}
else
{
//如果是跨越多行1列的情况,采用默认的paint
}
}
}
//painter.drawLine(viewport()->rect().bo