导航控件

本文档展示了NavWidget类的实现,它是一个包含鼠标事件处理的导航视图组件。NavWidget提供了设置和获取背景颜色、文字颜色、当前选中项颜色等属性的方法,并在鼠标按下和移动事件中更新当前选中项。代码中通过QPainter进行绘制,支持动态改变字体大小和字体家族。
摘要由CSDN通过智能技术生成

截图

在这里插入图片描述

NavWidget.h

#ifndef NAVWIDGET_H
#define NAVWIDGET_H

#include <QWidget>
#include <QVBoxLayout>
#include <QPushButton>
#include <QPainter>
#include <QMouseEvent>
class NavWidget : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(int currentIndex MEMBER m_CurrentIndex READ currentIndex WRITE setCurrentIndex)
    Q_PROPERTY(QColor backgroundColor MEMBER m_CurrentBackgroundColor READ backgroundColor WRITE setBackgroundColor)
    Q_PROPERTY(QColor textColor MEMBER m_TextColor READ textColor WRITE setTextColor)
    Q_PROPERTY(QColor currentTextColor MEMBER m_CurrentTextColor READ currentTextColor WRITE setCurrentTextColor)
    Q_PROPERTY(QColor currentBackgroundColor MEMBER m_CurrentBackgroundColor READ currentBackgroundColor WRITE setCurrentBackgroundColor)
    Q_PROPERTY(QColor hoverTextColor MEMBER m_HoverTextColor READ hoverTextColor WRITE setHoverTextColor)
    Q_PROPERTY(QColor hoverBackgroundColor MEMBER m_HoverBackgroundColor READ hoverBackgroundColor WRITE setHoverBackgroundColor)
    Q_PROPERTY(int pixelSize MEMBER m_PixelSize READ pixelSize WRITE setPixelSize)
    Q_PROPERTY(QString family MEMBER m_Family READ family WRITE setFamily)

public:
    explicit NavWidget(QWidget *parent = nullptr);
    int  currentIndex() const;
    QColor textColor() const;
    QColor backgroundColor() const;
    QColor currentTextColor() const;
    QColor currentBackgroundColor() const;
    QColor hoverTextColor() const;
    QColor hoverBackgroundColor() const;
    int pixelSize() const;
    QString family() const;
    void append(const QString str);
    bool empty() const;
    void removeAt(int i);
public slots:
    void setCurrentIndex(int index);
    void setBackgroundColor(QColor color);
    void setTextColor(QColor color);
    void setCurrentTextColor(QColor color);
    void setCurrentBackgroundColor(QColor color);
    void setHoverTextColor(QColor color);
    void setHoverBackgroundColor(QColor color);
    void setPixelSize(int size);
    void setFamily(QString family);
signals:
    void currentIndexChanged(int index);
protected:
    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
private:
    QStringList m_Item;
    int   m_HoverIndex;
    QFont m_Font;
    int   m_OldIndex;
private: //属性
    int m_CurrentIndex;
    QColor m_BackgroundColor;
    QColor m_TextColor;
    QColor m_CurrentBackgroundColor;
    QColor m_CurrentTextColor;
    QColor m_HoverTextColor;
    QColor m_HoverBackgroundColor;
    int    m_PixelSize;
    QString m_Family;
};

#endif // NAVWIDGET_H

NavWidget.cpp

#include "navwidget.h"

NavWidget::NavWidget(QWidget *parent) : QWidget(parent)
{
    m_CurrentIndex = 0;
    m_HoverIndex = 0;
    m_OldIndex = 0;
    m_TextColor = QColor(255, 255, 255);
    m_BackgroundColor = QColor(0, 0, 0);
    m_CurrentTextColor = QColor(0, 255, 0);
    m_CurrentBackgroundColor = QColor(0, 0, 0);
    m_HoverTextColor = QColor(0, 255, 255);
    m_HoverBackgroundColor = QColor(0, 0, 0);
    m_PixelSize = 16;
    m_Family = "宋体";
    m_Font.setPixelSize(m_PixelSize);
    m_Font.setFamily(m_Family);
    m_Font.setWeight(QFont::Normal);
    setMouseTracking(true);
}

int NavWidget::currentIndex() const
{
    return m_CurrentIndex;
}

void NavWidget::setCurrentIndex(int index)
{
    m_CurrentIndex = index;
}

QColor NavWidget::backgroundColor() const
{
    return m_BackgroundColor;
}

void NavWidget::setBackgroundColor(QColor color)
{
    m_BackgroundColor = color;
}

QColor NavWidget::textColor() const
{
    return m_TextColor;
}

void NavWidget::setTextColor(QColor color)
{
    m_TextColor = color;
}

QColor NavWidget::currentTextColor() const
{
    return m_CurrentTextColor;
}

void NavWidget::setCurrentTextColor(QColor color)
{
    m_CurrentTextColor = color;
}

QColor NavWidget::currentBackgroundColor() const
{
    return m_CurrentBackgroundColor;
}

void NavWidget::setCurrentBackgroundColor(QColor color)
{
    m_CurrentBackgroundColor = color;
}

QColor NavWidget::hoverTextColor() const
{
    return m_HoverTextColor;
}

void NavWidget::setHoverTextColor(QColor color)
{
    m_HoverTextColor = color;
}

QColor NavWidget::hoverBackgroundColor() const
{
    return m_HoverBackgroundColor;
}

void NavWidget::setHoverBackgroundColor(QColor color)
{
    m_HoverBackgroundColor = color;
}

int NavWidget::pixelSize() const
{
    return m_PixelSize;
}

void NavWidget::setPixelSize(int size)
{
    m_PixelSize = size;
    m_Font.setPixelSize(size);
    repaint();
}

QString NavWidget::family() const
{
    return m_Family;
}

void NavWidget::setFamily(QString family)
{
    m_Family = family;
    m_Font.setFamily(family);
    repaint();
}

void NavWidget::append(const QString str)
{
    m_Item.append(str);
}

bool NavWidget::empty() const
{
    return m_Item.empty();
}

void NavWidget::removeAt(int i)
{
    m_Item.removeAt(i);
}

void NavWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter(this);

    painter.fillRect(0, 0, width(), height(), QBrush(m_BackgroundColor));

    painter.setFont(m_Font);
    painter.setPen(m_TextColor);
    for (int i = 0; i < m_Item.count(); i++) {
        painter.drawText(0, m_PixelSize * i,  width(), m_PixelSize, Qt::AlignCenter, m_Item.at(i));
    }
        painter.setPen(m_CurrentTextColor);
        painter.fillRect(0, m_PixelSize * m_CurrentIndex,  width(), m_PixelSize, QBrush(m_CurrentBackgroundColor));
        if (m_CurrentIndex < m_Item.count())
            painter.drawText(0, m_PixelSize * m_CurrentIndex,  width(), m_PixelSize, Qt::AlignCenter, m_Item.at(m_CurrentIndex));
        painter.setPen(m_HoverTextColor);

        painter.fillRect(0, m_PixelSize * m_HoverIndex,  width(), m_PixelSize, QBrush(m_HoverBackgroundColor));
        if (m_HoverIndex < m_Item.count())
            painter.drawText(0, m_PixelSize * m_HoverIndex,  width(), m_PixelSize, Qt::AlignCenter, m_Item.at(m_HoverIndex));
}

void NavWidget::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
    m_CurrentIndex = event->pos().y() / m_PixelSize;
    if (m_Item.count() > 0 && m_CurrentIndex >= m_Item.count())
        m_CurrentIndex = m_Item.count() - 1;
    if (m_OldIndex != m_CurrentIndex) {
        m_OldIndex = m_CurrentIndex;
        currentIndexChanged(m_CurrentIndex);
    }
    repaint();
}

void NavWidget::mouseMoveEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
    m_HoverIndex = event->pos().y() / m_PixelSize;
    if (m_Item.count() > 0 && m_HoverIndex >= m_Item.count())
        m_HoverIndex = m_Item.count() - 1;
    repaint();
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值