前文链接:Qt动态多级导航菜单(二)
本次更新,主要将原导航项NavItem抽象为基类NavItemBase, 并派生出带有图标样式的动态多级导航菜单。至此,关于想要增加自定义的导航菜单样式,只需要从NavItemBase类派生即可。
效果图
核心代码
#pragma once
#include "NavItemBase.h"class NavItem2 : public NavItemBase
{Q_OBJECT
public:NavItem2(const QString &text, NavItemBase *parent = nullptr);~NavItem2();protected:virtual void paintEvent(QPaintEvent *event) override;
};
#include "NavItem2.h"
#include "QWHNavTreeView.h"
#include <QPainter>NavItem2::NavItem2(const QString &text, NavItemBase *parent /*= nullptr*/): NavItemBase(text, parent)
{}NavItem2::~NavItem2()
{}void NavItem2::paintEvent(QPaintEvent *event)
{Q_UNUSED(event)int width = this->width();int height = this->height();QPainter painter(this);painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);// 绘制背景painter.setPen((m_view->m_showTopItemBorder && 0 == m_level) ? QPen(m_selected ? m_view->m_borderSelectedColor : (m_hover ? m_view->m_borderHoverColor : m_view->m_borderColor), m_view->m_borderWidth) : Qt::NoPen);painter.setBrush(m_selected ? m_view->m_bgSelectedColor : (m_hover ? m_view->m_bgHoverColor : m_view->m_bgColor));// 判断是否是第一个顶层节点QPoint p = m_view->m_contentWidget->mapFromGlobal(this->mapToGlobal(QPoint(0, 0)));bool topFirst = p.y() == 0;painter.drawRect(rect().adjusted(-m_view->m_borderWidth, topFirst ? -m_view->m_borderWidth : -m_view->m_borderWidth / 2, m_view->m_borderWidth, 0));int iconSize = m_view->m_showIcon ? m_view->m_iconSize : 10;// 绘制折叠标志if (m_view->m_branchType == QWHNavTreeView::TRIANGLE){// 只有顶层节点绘制if (!m_parent){painter.save();int offsetY = 1;painter.translate(width - m_view->m_paddingLR - m_view->m_branchSize, height / 2 - offsetY);painter.rotate(m_childNum > 0 ? m_angle : -90);QPainterPath path;path.moveTo(-m_view->m_branchSize / 2, -m_view->m_branchSize * 0.3);path.lineTo(m_view->m_branchSize / 2, -m_view->m_branchSize * 0.3);path.lineTo(0, m_view->m_branchSize * 0.6);path.closeSubpath();painter.setPen(Qt::NoPen);painter.setBrush(m_selected ? m_view->m_branchSelectedColor : (m_hover ? m_view->m_branchHoverColor : m_view->m_branchColor));painter.drawPath(path);painter.restore();}}else// m_view->m_branchColor == QWHNavTreeView::CROSS{// 只有顶层节点绘制if (!m_parent){painter.save();painter.translate(width - m_view->m_paddingLR - m_view->m_branchSize, height / 2);painter.rotate(m_childNum > 0 ? m_angle : -90);QPainterPath path;path.addRect(-m_view->m_branchSize / 2, -NavContentWidget::LINE_WIDTH / 2, m_view->m_branchSize, NavContentWidget::LINE_WIDTH);if (0 != m_angle)path.addRect(-NavContentWidget::LINE_WIDTH / 2, -m_view->m_branchSize / 2, NavContentWidget::LINE_WIDTH, m_view->m_branchSize);painter.setPen(Qt::NoPen);painter.setBrush(m_selected ? m_view->m_branchSelectedColor : (m_hover ? m_view->m_branchHoverColor : m_view->m_branchColor));painter.drawPath(path);painter.restore();}}// 绘制文本int offsetX = 6;int textW = width - (m_view->m_paddingLR + m_view->m_branchSize + iconSize + offsetX);QRect textRect(m_view->m_paddingLR + iconSize / 2 * 5, 0, textW, height);QFont font = painter.font();font.setPixelSize(20);painter.setFont(font);painter.setPen(QPen(m_selected ? m_view->m_selectedColor : (m_hover ? m_view->m_hoverColor : m_view->m_color), 2));painter.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, m_text);// 绘制图标if (m_view->m_showIcon){QRect iconRect(m_view->m_paddingLR + QWHNavTreeView::LEVEL_X * m_level + iconSize / 2, (height - iconSize) / 2, iconSize, iconSize);painter.drawPixmap(iconRect, m_icon);}
}