#include "multiLineHeaderView.h" #include MultiLineHeaderView::MultiLineHeaderView(Qt::Orientation orientation, QWidget* parent) : QHeaderView(orientation, parent) { m_linSpacing = 2; m_bgColor = QColor(Qt::white); m_borderColor = QColor(Qt::lightGray); setSectionResizeMode(QHeaderView::ResizeToContents);//根据内容自动调整大小 } void MultiLineHeaderView::setLineSpacing(int spacing) { m_linSpacing = spacing; } void MultiLineHeaderView::setBackgroundColor(QColor& color) { m_bgColor = color; } void MultiLineHeaderView::setBorderColor(QColor& color) { m_borderColor = color; } void MultiLineHeaderView::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const { if(!painter->isActive()) return; //绘制背景 painter->fillRect(rect, m_bgColor); //获取文本 QString text = model()->headerData(logicalIndex, orientation()).toString(); QStringList lines = text.split('\n'); //获取当前列的样式 QMap styles = m_sectionStyles.value(logicalIndex); //计算总高度并垂直居中 int totalHeight = 0; QVector lineHeights; for(int i = 0; i < lines.size(); i++) { HeaderLineStyle style = styles.value(i, m_defaultStyle); QFontMetrics fm(style.font); lineHeights.append(fm.height()); totalHeight += fm.height() + (i < lines.size() - 1 ? m_linSpacing : 0); } int y = rect.y() + (rect.height() - totalHeight) * 0.5; //逐行绘制文本 for(int i = 0; i < lines.size(); i++) { const QString& line = lines.at(i); HeaderLineStyle style = styles.value(i, m_defaultStyle); QFontMetrics fm(style.font); //水平位置 int padding = 10; int x = rect.x() + padding; //左对齐 if(style.alignment & Qt::AlignRight) //右对齐 x = rect.right() - fm.horizontalAdvance(line) - 4;//'horizontalAdvance'计算文本的横向距离 else if(style.alignment & Qt::AlignHCenter) //水平居中 x = rect.x() + (rect.width() - fm.horizontalAdvance(line)) * 0.5; //设置字体和颜色 painter->setFont(style.font); painter->setPen(style.color); //绘制文本 painter->drawText(x, y + fm.ascent(), line); //绘制文本的基线X轴离文本上方的距离为ascent,下方距离为descent y = y + lineHeights[i] + m_linSpacing; } //绘制边框 QPen pen(m_borderColor, 1); painter->setPen(pen); QRect adjustRect = rect.adjusted(0, 0, 1, 0); //painter->drawLine(adjustRect.topLeft(), adjustRect.topRight()); painter->drawLine(adjustRect.bottomLeft(), adjustRect.bottomRight()); painter->drawLine(adjustRect.topRight(), adjustRect.bottomRight()); } //自动调整行高 QSize MultiLineHeaderView::sectionSizeFromContents(int logicalIndex) const { //获取文本 QString text = model()->headerData(logicalIndex, orientation()).toString(); //获取当前列的样式 QMap styles = m_sectionStyles.value(logicalIndex); QStringList lines = text.split('\n'); int maxWidth = 0; int totalHeight = 0; for(int i = 0; i < lines.size(); i++) { HeaderLineStyle style = styles.value(i, m_defaultStyle); QFontMetrics fm(style.font); maxWidth = qMax(maxWidth, fm.horizontalAdvance(lines[i])); totalHeight += fm.height() + (i < lines.size() - 1 ? m_linSpacing : 0); } //qDebug() << maxWidth << "," << totalHeight; return QSize(maxWidth + 20, totalHeight + 4); } void MultiLineHeaderView::setSectionLineStyle(int section, int line, const HeaderLineStyle& style) { m_sectionStyles[section][line] = style; updateSection(section); }