Qt-绘制个稍微立体的方向按钮

1.操作3D视图需要个立体点的方向按钮

2.现在SolidWork绘制草图

3.把3D轴测图投影到平面之后绘制出来

#ifndef MYDIRECTIONBUTTON_H
#define MYDIRECTIONBUTTON_H

#include<QWidget>

class myDirectionButton : public QWidget
{
    Q_OBJECT

public:
    enum PRESSED {
        HOME,
        ZN,
        XN,
        ZP,
        XP,
        YP,
        YN,
        RN,
        RP,
        NONE
    };

    myDirectionButton(QWidget *parent = nullptr);

    void setPressed(int button);

signals:
    void signalPressed(int button);

protected:
    void mousePressEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void paintEvent(QPaintEvent *event)override;
    void resizeEvent(QResizeEvent *event) override;

private:
    int m_pressedBtn;
    QColor m_normalClr;
    QColor m_pressedClr;
    double m_scale;

    QVector<QPainterPath>m_buttons;     //按钮区域

    void createButtons();               //创建按钮路径

};

#endif // MYDIRECTIONBUTTON_H

#include "myDirectionButton.h"

#include<QDebug>
#include<QPainter>
#include<QMouseEvent>


myDirectionButton::myDirectionButton(QWidget *parent)
    : QWidget(parent)
{
    m_scale = 1.0;
    m_pressedBtn = NONE;
    m_normalClr = QColor(200, 255, 200, 200);
    m_pressedClr = QColor(100, 255, 100);
    m_buttons.resize(9);


    setMinimumSize(100, 100);
    createButtons();
}

void myDirectionButton::createButtons()
{
    //Z-按钮路径
    QPainterPath btnZN;
    btnZN.moveTo(51.8031, -17.6591);
    btnZN.lineTo(30.8108, -30.0328);
    btnZN.lineTo(49.4975, -40.8204);
    btnZN.lineTo(38.8909, -46.9435);
    btnZN.lineTo(77.7817, -44.9024);
    btnZN.lineTo(81.3173, -22.4512);
    btnZN.lineTo(70.7107, -28.5743);
    btnZN.closeSubpath();

    QPainterPath ellipse;
    ellipse.addEllipse(-60, -35, 120, 70);
    btnZN = btnZN.subtracted(ellipse);

    //Y+按钮路径
    QPainterPath btnYP;
    btnYP.moveTo(15, -20);
    btnYP.arcTo(QRectF(-15, -27.5, 30, 15), 0, -180);
    btnYP.lineTo(-15, -53.0752);
    btnYP.lineTo(-30, -53.0752);
    btnYP.lineTo(0, -73.4887);
    btnYP.lineTo(30, -53.0752);
    btnYP.lineTo(15, -53.0752);
    btnYP.closeSubpath();

    //Y-按钮路径
    QPainterPath btnYN;
    btnYN.moveTo(15, 33.8373);
    btnYN.lineTo(-15, 33.9405);
    btnYN.lineTo(-15, 53.0752);
    btnYN.lineTo(-30, 53.0752);
    btnYN.lineTo(0, 73.4887);
    btnYN.lineTo(30, 53.0752);
    btnYN.lineTo(15, 53.0752);
    btnYN.closeSubpath();
    btnYN = btnYN.subtracted(ellipse);

    //A+按钮路径
    QPainterPath btnRP;
    btnRP.moveTo(60, 0);
    btnRP.arcTo(QRectF(-60, -35, 120, 70), 0, -15);
    btnRP.lineTo(74.5388, 19.9726);
    btnRP.arcTo(QRectF(-80, -55, 160, 110), -15, 15);
    btnRP.lineTo(85, 0);
    btnRP.lineTo(66.2124, -17.7415);
    btnRP.lineTo(55, 0);
    btnRP.closeSubpath();

    //回原按钮
    QPainterPath home;
    home.addEllipse(-50, -25, 100, 50);

    //路径转换
    m_buttons[ZN] = btnZN;
    m_buttons[YP] = btnYP;
    m_buttons[YN] = btnYN;
    m_buttons[RP] = btnRP;
    m_buttons[HOME] = home;

    //Y轴镜像
    QTransform mirrY;
    mirrY.scale(-1, 1);
    m_buttons[XN] = mirrY.map(btnZN);
    m_buttons[RN] = mirrY.map(btnRP);

    //X轴镜像
    QTransform mirrX;
    mirrX.scale(1, -1);
    m_buttons[XP] = mirrX.map(btnZN);

    //180旋转
    QTransform rotate;
    rotate.rotate(180);
    m_buttons[ZP] = rotate.map(btnZN);
}


void myDirectionButton::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);

    QPainter painter(this);
    //坐标系到窗口中心
    painter.translate(size().width() / 2, size().height() / 2);
    painter.scale(m_scale, m_scale);

    //绘制按钮区
    painter.setBrush(m_normalClr);
    for(int i = 0; i < m_buttons.count(); i++) {
        painter.drawPath(m_buttons.at(i));
    }

    //根据状态绘制不同颜色
    if(m_pressedBtn != NONE)
    {
        painter.setBrush(m_pressedClr);
        painter.drawPath(m_buttons[m_pressedBtn]);
    }

    //绘制文字
    QFont font = painter.font();
    font.setPixelSize(20);
    painter.setFont(font);
    painter.setPen(QPen(Qt::black, 2, Qt::SolidLine));
    painter.drawText(QRectF(-45, -45, 90, 90), Qt::AlignCenter, "HOME");

    font.setPixelSize(15);
    painter.setFont(font);
    painter.drawText(QRectF(55, -45, 20, 20), Qt::AlignCenter, "Z-");
    painter.drawText(QRectF(-65, -45, 20, 20), Qt::AlignCenter, "X-");
    painter.drawText(QRectF(-65, 25, 20, 20), Qt::AlignCenter, "Z+");
    painter.drawText(QRectF(55, 25, 20, 20), Qt::AlignCenter, "X+");
    painter.drawText(QRectF(-5, -55, 15, 10), Qt::AlignCenter, "Y+");
    painter.drawText(QRectF(-5, 50, 15, 10), Qt::AlignCenter, "Y-");
    painter.drawText(QRectF(65, -5, 15, 10), Qt::AlignCenter, "R+");
    painter.drawText(QRectF(-75, -5, 15, 10), Qt::AlignCenter, "R-");

}

void myDirectionButton::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);

    //默认绘图区域200X160,窗口变化时缩放
    m_scale = qMin(size().width() / 200.0, size().height() / 160.0);
}

void myDirectionButton::mouseReleaseEvent(QMouseEvent *)
{
    setPressed(NONE);
}

void myDirectionButton::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        QTransform trans;
        trans.scale(1 / m_scale, 1 / m_scale);
        trans.translate(-width() / 2, -height() / 2);
        QPoint p = trans.map(event->pos());

        for(int index = 0; index < m_buttons.count(); index++)
        {
            if(m_buttons[index].contains(p))
            {
                setPressed(index);
                return ;
            }
        }
    }
}

void myDirectionButton::setPressed(int button)
{
    if(button != m_pressedBtn)
    {
        m_pressedBtn = button;
        emit signalPressed(button);
        update();
    }
}


4.根据需要复制代码,自行修改

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值