1 Star 0 Fork 0

uthelei/CodeLineCount

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
headerview.cpp 5.92 KB
一键复制 编辑 原始数据 按行查看 历史
uthelei 提交于 2023-05-25 20:26 . 修改表格居中
#include "headerview.h"
#include <QPaintEvent>
#include <QPainter>
#include <QPainterPath>
#include <QApplication>
//#include <DApplicationHelper>
#include <QPalette>
//#include <QStyleHelper>
#include <QStyle>
//DWIDGET_USE_NAMESPACE
static const int kSpacingMargin = 4;
HeaderView::HeaderView(Qt::Orientation orientation, QWidget* parent)
: QHeaderView(orientation, parent)
{
viewport()->setAutoFillBackground(false);
setStretchLastSection(true);
setSectionResizeMode(QHeaderView::Interactive);
setDefaultAlignment(Qt::AlignCenter);
setFixedHeight(40);
setFocusPolicy(Qt::StrongFocus);
setMouseTracking(true);
}
void HeaderView::paintSection(QPainter* painter, const QRect &rect, int logicalIndex) const
{
painter->save();
painter->setRenderHint(QPainter::Antialiasing);
painter->setOpacity(1);
QPalette::ColorGroup cg;
QWidget* wnd = qApp->activeWindow();
if (!wnd)
cg = QPalette::Inactive;
else
cg = QPalette::Active;
// DApplicationHelper *dAppHelper = DApplicationHelper::instance();
QPalette palette = qApp->palette();
QStyle* style = QApplication::style();
QStyleOptionHeader option;
initStyleOption(&option);
int margin = 8;
// title
QRect contentRect(rect.x(), rect.y(), rect.width(), rect.height() - m_spacing);
QRect hSpacingRect(rect.x(), contentRect.height(), rect.width(), rect.height() - contentRect.height());
QBrush contentBrush(palette.color(cg, QPalette::Base));
// horizontal spacing
QBrush hSpacingBrush(Qt::black);
// vertical spacing
QBrush vSpacingBrush(Qt::black);
QRectF vSpacingRect(rect.x(), rect.y() + kSpacingMargin, m_spacing, rect.height() - kSpacingMargin * 2);
QBrush clearBrush(palette.color(cg, QPalette::Window));
painter->fillRect(hSpacingRect, clearBrush);
painter->fillRect(hSpacingRect, hSpacingBrush);
if (visualIndex(logicalIndex) > 0)
{
painter->fillRect(vSpacingRect, clearBrush);
painter->fillRect(vSpacingRect, vSpacingBrush);
}
QPen forground;
forground.setColor(palette.color(cg, QPalette::Text));
// 绘制文字
QRect textRect;
if (sortIndicatorSection() == logicalIndex)
{
textRect = {contentRect.x() + margin, contentRect.y(), contentRect.width() - margin * 3 - 8,
contentRect.height()
};
}
else
{
textRect = {contentRect.x() + margin, contentRect.y(), contentRect.width() - margin, contentRect.height()};
}
if (!model())
return;
QString title = model()->headerData(logicalIndex, orientation(), Qt::DisplayRole).toString();
painter->setPen(forground);
if (logicalIndex == 0)
{
QRect col0Rect = textRect;
col0Rect.setX(textRect.x() /*+ margin*/ - 6);
painter->drawText(col0Rect, static_cast<int>(defaultAlignment()), title);
}
else
{
painter->drawText(textRect, static_cast<int>(defaultAlignment()), title);
}
if (isSortIndicatorShown() && logicalIndex == sortIndicatorSection())
{
// 绘制排序的箭头图标(8×5)
QRect sortIndicator(textRect.x() + textRect.width() + margin, textRect.y() + (textRect.height() - 10) / 2, 12,
10);
option.rect = sortIndicator;
if (sortIndicatorOrder() == Qt::DescendingOrder)
{
style->drawPrimitive(QStyle::PE_IndicatorArrowDown, &option, painter, this);
}
else if (sortIndicatorOrder() == Qt::AscendingOrder)
{
style->drawPrimitive(QStyle::PE_IndicatorArrowUp, &option, painter, this);
}
else
{
}
}
painter->restore();
}
void HeaderView::focusInEvent(QFocusEvent* event)
{
m_reson = event->reason();
QHeaderView::focusInEvent(event);
}
void HeaderView::mouseMoveEvent(QMouseEvent* event)
{
emit headerViewMouseMove();
QHeaderView::mouseMoveEvent(event);
}
void HeaderView::paintEvent(QPaintEvent* event)
{
QPainter painter(viewport());
painter.save();
QPalette::ColorGroup cg;
//是否有为激活窗口状态
QWidget* wnd = qApp->activeWindow();
if (!wnd)
cg = QPalette::Inactive;
else
cg = QPalette::Active;
// DApplicationHelper *dAppHelper = DApplicationHelper::instance();
QPalette palette = qApp->palette();
QStyle* style = QApplication::style();
QBrush bgBrush(palette.color(cg, QPalette::Base));
QStyleOptionHeader option;
initStyleOption(&option);
int radius = 8;
QRect rect = viewport()->rect();
QRectF clipRect(rect.x(), rect.y(), rect.width(), rect.height() * 2);
QRectF subRect(rect.x(), rect.y() + rect.height(), rect.width(), rect.height());
QPainterPath clipPath, subPath;
clipPath.addRoundedRect(clipRect, radius, radius);
subPath.addRect(subRect);
clipPath = clipPath.subtracted(subPath);
painter.fillPath(clipPath, bgBrush);
QHeaderView::paintEvent(event);
painter.restore();
// draw focus
if (hasFocus() && (m_reson == Qt::TabFocusReason || m_reson == Qt::BacktabFocusReason))
{
QStyleOptionFocusRect o;
o.QStyleOption::operator=(option);
QRect focusRect {rect.x() - offset(), rect.y(), length() - sectionPosition(0), rect.height()};
o.rect = style->visualRect(layoutDirection(), rect, focusRect);
style->drawPrimitive(QStyle::PE_FrameFocusRect, &o, &painter);
}
}
QSize HeaderView::sizeHint() const
{
return QSize(width(), 36 + m_spacing);
}
int HeaderView::sectionSizeHint(int logicalIndex) const
{
QStyleOptionHeader option;
initStyleOption(&option);
int margin = 8;
QFontMetrics fm(QApplication::font());
if (!model())
return -1;
QString buf = model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString();
if (sortIndicatorSection() == logicalIndex)
return fm.width(buf) + margin * 3 + 8;
else
return fm.width(buf) + margin * 2;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/uthelei/CodeLineCount.git
git@gitee.com:uthelei/CodeLineCount.git
uthelei
CodeLineCount
CodeLineCount
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385