QT实现一个摇杆手柄

这篇具有很好参考价值的文章主要介绍了QT实现一个摇杆手柄。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在智能小车的控制里,最好用的控制方式还是手柄控制,但是手柄的价格又相对较贵,今天就给大家讲解如何只用Qt实现一个摇杆手柄;该软件摇杆手柄支持固定方向单次点击控制,也支持组合方向滑动控制,并且滑动控制下会显示当前滑动位置的X轴数据和Y轴数据,具体的操作演示,大家可以查看文章后面的功能演示视频,可以更直观的了解该手柄的功能。下面主要对如何实现的进行讲解。

qt 摇杆,开源智能小车(ROS小车)搭建,Qt,qt,ui,开发语言,Powered by 金山文档

第一步:需要创建一个Qt工程,这步就跳过了

第二步:在ui文件中添加几个控件

qt 摇杆,开源智能小车(ROS小车)搭建,Qt,qt,ui,开发语言,Powered by 金山文档

第三步:编写一个类(CustomBox),该类则是手柄控制的核心

#include "custombox.h"
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>
#include <QPushButton>
#include <QToolTip>
#include <qmath.h>

CustomBox::CustomBox(QWidget* parent)
    : QWidget(parent)
    , m_radius(90)              //小圆半径
    , m_arcLength(80)           //小圆弧长
    , m_pressIndex(0)
    , m_enterIndex(0)
    , m_isMousePressed(false)
    , m_isMouseEntered(false)
    , mCurWorkRegion(QUADRANT_NO_DEFINE)
    , mCenterRound(QPoint(0,0))
    , SliderValue_t(0.2)
    , ControlMode(0) //0空闲模式,1自由模式,2固定模式
    , boxSendTimer(new QTimer)
{
    connect(boxSendTimer,SIGNAL(timeout()), this, SLOT(boxSendTimer_clicked()));
    m_bTextModeEn = false;
    this->setMouseTracking(true);
    mSectorColor = QColor(38,38,38);
    initButton();
    setWidgetStyle();
    setAxesVertical();
}

void CustomBox::setWidgetStyle(void)
{
    mSectorColor = QColor(255,0,0);
    colorSPL = QColor(32, 149, 216);
    colorBKG = QColor(41, 44, 50);

    colorSectorUp2 = QColor(68, 68, 68);
    colorSectorUp = QColor(60, 60, 60);
    colorSectorDown = QColor(22, 22, 22);

    colorbgGradient0 = QColor(24, 24, 24);
    colorbgGradient1 = QColor(53, 57, 63);

    colorExcircle0 = QColor(255, 0, 0);
    colorExcircle5 = QColor(255, 0, 0);
    colorExcircle9 = QColor(255, 0, 0);

    colorInnerCircle0 = QColor(45, 48, 56);
    colorInnerCircle9 = QColor(30, 32, 37);
    update();
}

void CustomBox::setRadiusValue(int radius)
{   //设置小圆半径
    m_radius = radius;
}

void CustomBox::setArcLength(int arcLength)
{   //设置小圆弧长
    m_arcLength = arcLength;
}

void CustomBox::drawRotatedText(QPainter *painter, float degrees, int x, int y, const QString &text)
{
    painter->save();                //保存原来坐标系统
    painter->translate(x, y);       //平移坐标原点到 x, y
    painter->rotate(degrees);       //坐标旋转degrees 度
    painter->drawText(0, 0, text);  //在原点绘制文本
    painter->restore();             //回复原来的坐标系统
}

void CustomBox::setAxesVertical(void)
{
    addArc(1,0,45, 90, mSectorColor);
    addArc(0,1,135, 90, mSectorColor);
    addArc(-1,0,225, 90, mSectorColor);
    addArc(0,-1,315, 90, mSectorColor);
    // 绘制中心圆;
    QPainterPath centerRoundPath;
    centerRoundPath.addEllipse(QPoint(0, 0), m_radius - m_arcLength+2, m_radius - m_arcLength+2);
    m_arcPathList.append(centerRoundPath);
    m_colorList.append(QColor(255, 255, 255));

    // 添加文字;
    QFont font;
    font.setFamily("Microsoft YaHei");
    font.setPointSize(14);
    update();
}

void CustomBox::initButton()
{
    addArc(1,0,90, 90, mSectorColor);
    addArc(1,0,45, 90, mSectorColor);
    addArc(0,1,135, 90, mSectorColor);
    addArc(-1,0,225, 90, mSectorColor);
    addArc(0,-1,315, 90, mSectorColor);

    // 绘制中心圆;
    QPainterPath centerRoundPath;
    centerRoundPath.addEllipse(QPoint(0, 0), m_radius - m_arcLength+2, m_radius - m_arcLength+2);
    m_arcPathList.append(centerRoundPath);
    m_colorList.append(QColor(255, 255, 255));
}

void CustomBox::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
    painter.setPen(Qt::NoPen);
    painter.translate(width()>>1, height()>>1);
    //背景色,分割线颜色
    painter.setBrush(colorBKG);
    painter.drawEllipse(QPoint(0,0), m_radius+8, m_radius+8);

    QLinearGradient linearGradient(0, -m_radius-2, 0, m_radius+2);
    linearGradient.setColorAt(0.0, colorSectorUp2);
    linearGradient.setColorAt(0.9, colorSectorDown);
    painter.setBrush(QBrush(linearGradient));
    painter.drawEllipse(QPoint(0, 0), m_radius+2, m_radius+2);

    linearGradient = QLinearGradient(0, -m_radius, 0, m_radius);
    linearGradient.setColorAt(0.0, colorSectorUp);
    linearGradient.setColorAt(0.9, colorSectorDown);
    painter.setBrush(QBrush(linearGradient));
    painter.drawEllipse(QPoint(0, 0), m_radius, m_radius);

    QLinearGradient bgGradient(0, -20, 0, 20);
    bgGradient.setColorAt(0.0, colorbgGradient0);
    bgGradient.setColorAt(1.0, colorbgGradient1);
    painter.setBrush(bgGradient);
    painter.drawEllipse(QPoint(0, 0), m_radius - m_arcLength+4, m_radius - m_arcLength+4);

    //画分割线
    int count = 4;
    for(int i = 0; i < count;i++)
    {
        painter.save();
        painter.rotate(0+90 * i);
        painter.setPen(QPen(colorBKG, 3, Qt::SolidLine));
        painter.drawLine(0, m_radius - m_arcLength+5, 0,m_radius+5);
        painter.setPen(QPen(colorSPL, 3, Qt::SolidLine, Qt::RoundCap));
        painter.drawLine(0, m_radius - m_arcLength+6, 0, 40);
        painter.setPen(QPen(colorSPL, 3, Qt::SolidLine));
        painter.drawLine(0, 40, 0, m_radius - 5);
        painter.restore();
    }
    count = 8;
    for(int i = 0; i < count;i++)
    {
        painter.save();
        painter.rotate(45 * i);
        painter.setPen(QPen(QColor(255,0,0), 3, Qt::SolidLine));        //分割线底色轮廓
        painter.drawLine(0, m_radius - 20, 0,m_radius);                 //中间圆底色轮廓填充
        painter.restore();
    }
    for(int i = 0; i < count;i++)
    {
        painter.save();
        painter.rotate(45 * i);
        QPainterPath drawtriangle;  //单独画三角形
        drawtriangle.moveTo(-10, m_radius-10);//左下角,第一点坐标为(0,height);
        drawtriangle.lineTo(0, m_radius+2);//第二点坐标为(width/2,width/2)
        drawtriangle.lineTo(10,  m_radius-10);//右下角,第三坐标(width, height),移动到右下角结束点,整体形成一个闭合路径
        if(mCurWorkRegion == QUADRANT_DOWN && i==0){
            painter.setBrush(Qt::green);   //填充绿色
        }else if(mCurWorkRegion == QUADRANT_DOWN_LEFT && i==1){
            painter.setBrush(Qt::green);   //填充绿色
        }else if(mCurWorkRegion == QUADRANT_LEFT && i==2){
            painter.setBrush(Qt::green);   //填充绿色
        }else if(mCurWorkRegion == QUADRANT_UP_LEFT && i==3){
            painter.setBrush(Qt::green);   //填充绿色
        }else if(mCurWorkRegion == QUADRANT_UP && i==4){
            painter.setBrush(Qt::green);   //填充绿色
        }else if(mCurWorkRegion == QUADRANT_UP_RIGHT && i==5){
            painter.setBrush(Qt::green);   //填充绿色
        }else if(mCurWorkRegion == QUADRANT_RIGHT && i==6){
            painter.setBrush(Qt::green);   //填充绿色
        }else if(mCurWorkRegion == QUADRANT_DOWN_RIGHT && i==7){
            painter.setBrush(Qt::green);   //填充绿色
        }else{
            painter.setBrush(QColor(128,128,128));   //填充绿色
        }
        painter.drawPath(drawtriangle);  //绘制出图形
        painter.restore();
    }
    linearGradient = QLinearGradient(0, mCenterRound.y() - m_radius + m_arcLength - 1, 0, mCenterRound.y()+m_radius - m_arcLength + 1);
    linearGradient.setColorAt(0.0, colorExcircle0);
    linearGradient.setColorAt(0.0, colorExcircle5);
    linearGradient.setColorAt(0.9, colorExcircle9);

    painter.setBrush(QBrush(linearGradient));//中间圆跟着移动
    painter.drawEllipse(mCenterRound, m_radius - m_arcLength , m_radius - m_arcLength );
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);

    //绘制占比线
    if(objPointValue.y()<0 && objPointValue.x()==0){    //后退占比线
        painter.save();
        QMatrix leftmatriy;
        leftmatriy.rotate(180);
        mDegreePixmap_y=mDegreePixmap_y.transformed(leftmatriy,Qt::SmoothTransformation);
        painter.drawPixmap(-30,30,60,30,mDegreePixmap_y);
        painter.restore();
        QFont font;
        font.setFamily("Microsoft YaHei");font.setPointSize(10);painter.setFont(font);painter.setPen(QColor(255,255,0));
        painter.save();painter.rotate(0);painter.drawText(-15, -20,"0.00");painter.restore();
        painter.save();painter.rotate(90);painter.drawText(-15, -20,showVel_xValue);painter.restore();
        painter.save();painter.rotate(0);painter.drawText(-15, 30,showAng_yValue);painter.restore();
        painter.save();painter.rotate(270);painter.drawText(-15, -20,"0.00");painter.restore();
    }
    if(objPointValue.y()<0 && objPointValue.x()<0){     //左后占比线
        painter.save();
        QMatrix leftmatrix;
        leftmatrix.rotate(270);
        mDegreePixmap_x=mDegreePixmap_x.transformed(leftmatrix,Qt::SmoothTransformation);
        painter.drawPixmap(-60,-30,30,60,mDegreePixmap_x);

        QMatrix leftmatriy;
        leftmatriy.rotate(180);
        mDegreePixmap_y=mDegreePixmap_y.transformed(leftmatriy,Qt::SmoothTransformation);
        painter.drawPixmap(-30,30,60,30,mDegreePixmap_y);painter.restore();
        QFont font;
        font.setFamily("Microsoft YaHei");font.setPointSize(10);painter.setFont(font);painter.setPen(QColor(255,255,0));
        painter.save();painter.rotate(0);painter.drawText(-15, -20,"0.00");painter.restore();
        painter.save();painter.rotate(90);painter.drawText(-15, -20,"0.00");painter.restore();
        painter.save();painter.rotate(0);painter.drawText(-15, 30,showAng_yValue);painter.restore();
        painter.save();painter.rotate(270);painter.drawText(-15, -20,showVel_xValue);painter.restore();
    }
    if(objPointValue.y()<0 && objPointValue.x()>0){     //右后占比线
        painter.save();
        QMatrix leftmatrix;
        leftmatrix.rotate(90);
        mDegreePixmap_x=mDegreePixmap_x.transformed(leftmatrix,Qt::SmoothTransformation);
        painter.drawPixmap(30,-30,30,60,mDegreePixmap_x);
        QMatrix leftmatriy;
        leftmatriy.rotate(180);
        mDegreePixmap_y=mDegreePixmap_y.transformed(leftmatriy,Qt::SmoothTransformation);
        painter.drawPixmap(-30,30,60,30,mDegreePixmap_y);painter.restore();
        QFont font;
        font.setFamily("Microsoft YaHei");font.setPointSize(10);painter.setFont(font);painter.setPen(QColor(255,255,0));
        painter.save();painter.rotate(0);painter.drawText(-15, -20,"0.00");painter.restore();
        painter.save();painter.rotate(90);painter.drawText(-15, -20,showVel_xValue);painter.restore();
        painter.save();painter.rotate(0);painter.drawText(-15, 30,showAng_yValue);painter.restore();
        painter.save();painter.rotate(270);painter.drawText(-15, -20,"0.00");painter.restore();
    }
    if(objPointValue.y()>0 && objPointValue.x()==0){    //前进占比线
        painter.save();
        QMatrix leftmatriy;
        leftmatriy.rotate(360);
        mDegreePixmap_y=mDegreePixmap_y.transformed(leftmatriy,Qt::SmoothTransformation);
        painter.drawPixmap(-30,-60,60,30,mDegreePixmap_y);painter.restore();
        QFont font;
        font.setFamily("Microsoft YaHei");font.setPointSize(10);painter.setFont(font);painter.setPen(QColor(255,255,0));
        painter.save();painter.rotate(0);painter.drawText(-15, -20,showAng_yValue);painter.restore();
        painter.save();painter.rotate(90);painter.drawText(-15, -20,"0.00");painter.restore();
        painter.save();painter.rotate(0);painter.drawText(-15, 30,"0.00");painter.restore();
        painter.save();painter.rotate(270);painter.drawText(-15, -20,"0.00");painter.restore();
    }
    if(objPointValue.y()>0 && objPointValue.x()<0){     //左前占比线
        painter.save();
        QMatrix leftmatrix;
        leftmatrix.rotate(270);
        mDegreePixmap_x=mDegreePixmap_x.transformed(leftmatrix,Qt::SmoothTransformation);
        painter.drawPixmap(-60,-30,30,60,mDegreePixmap_x);
        QMatrix leftmatriy;
        leftmatriy.rotate(360);
        mDegreePixmap_y=mDegreePixmap_y.transformed(leftmatriy,Qt::SmoothTransformation);
        painter.drawPixmap(-30,-60,60,30,mDegreePixmap_y);painter.restore();
        QFont font;
        font.setFamily("Microsoft YaHei");font.setPointSize(10);painter.setFont(font);painter.setPen(QColor(255,255,0));
        painter.save();painter.rotate(0);painter.drawText(-15, -20,showAng_yValue);painter.restore();
        painter.save();painter.rotate(90);painter.drawText(-15, -20,"0.00");painter.restore();
        painter.save();painter.rotate(0);painter.drawText(-15, 30,"0.00");painter.restore();
        painter.save();painter.rotate(270);painter.drawText(-15, -20,showVel_xValue);painter.restore();

    }
    if(objPointValue.y()>0 && objPointValue.x()>0){     //右前占比线
        painter.save();
        QMatrix leftmatrix;
        leftmatrix.rotate(90);
        mDegreePixmap_x=mDegreePixmap_x.transformed(leftmatrix,Qt::SmoothTransformation);
        painter.drawPixmap(30,-30,30,60,mDegreePixmap_x);
        QMatrix leftmatriy;
        leftmatriy.rotate(360);
        mDegreePixmap_y=mDegreePixmap_y.transformed(leftmatriy,Qt::SmoothTransformation);
        painter.drawPixmap(-30,-60,60,30,mDegreePixmap_y);painter.restore();
        QFont font;
        font.setFamily("Microsoft YaHei");font.setPointSize(10);painter.setFont(font);painter.setPen(QColor(255,255,0));
        painter.save();painter.rotate(0);painter.drawText(-15, -20,showAng_yValue);painter.restore();
        painter.save();painter.rotate(90);painter.drawText(-15, -20,showVel_xValue);painter.restore();
        painter.save();painter.rotate(0);painter.drawText(-15, 30,"0.00");painter.restore();
        painter.save();painter.rotate(270);painter.drawText(-15, -20,"0.00");painter.restore();
    }
    if(objPointValue.y()==0 && objPointValue.x()>0){    //右占比线
        painter.save();
        QMatrix leftmatrix;
        leftmatrix.rotate(90);
        mDegreePixmap_x=mDegreePixmap_x.transformed(leftmatrix,Qt::SmoothTransformation);
        painter.drawPixmap(30,-30,30,60,mDegreePixmap_x);painter.restore();
        QFont font;
        font.setFamily("Microsoft YaHei");font.setPointSize(10);painter.setFont(font);painter.setPen(QColor(255,255,0));
        painter.save();painter.rotate(0);painter.drawText(-15, -20,"0.00");painter.restore();
        painter.save();painter.rotate(90);painter.drawText(-15, -20,showVel_xValue);painter.restore();
        painter.save();painter.rotate(0);painter.drawText(-15, 30,"0.00");painter.restore();
        painter.save();painter.rotate(270);painter.drawText(-15, -20,"0.00");painter.restore();
    }
    if(objPointValue.y()==0 && objPointValue.x()<0){    //左占比线
        painter.save();
        QMatrix leftmatrix;
        leftmatrix.rotate(270);
        mDegreePixmap_x=mDegreePixmap_x.transformed(leftmatrix,Qt::SmoothTransformation);
        painter.drawPixmap(-60,-30,30,60,mDegreePixmap_x);painter.restore();
        QFont font;
        font.setFamily("Microsoft YaHei");font.setPointSize(10);painter.setFont(font);painter.setPen(QColor(255,255,0));
        painter.save();painter.rotate(0);painter.drawText(-15, -20,"0.00");painter.restore();
        painter.save();painter.rotate(90);painter.drawText(-15, -20,"0.00");painter.restore();
        painter.save();painter.rotate(0);painter.drawText(-15, 30,"0.00");painter.restore();
        painter.save();painter.rotate(270);painter.drawText(-15, -20,showVel_xValue);painter.restore();
    }
    if(!boxSendTimer->isActive()){
        boxSendTimer->start(20);
    }
}
//设置小圆路径
void CustomBox::addArc(int x, int y, int startAngle, int angleLength, QColor color)
{
    //绘制矩形 m_radius = 90
    //    QRectF(qreal left, qreal top, qreal width, qreal height);
    QRectF rect(-m_radius+x, -m_radius+y, m_radius * 2, m_radius * 2);
    // 设置扇形路径;
    QPainterPath path;
    path.arcTo(rect, startAngle, angleLength);
    QPainterPath subPath;
    // 设置小扇形路径;
    subPath.addEllipse(rect.adjusted(m_arcLength, m_arcLength, -m_arcLength, -m_arcLength));
    // 大扇形减去小扇形得到圆弧;
    path -= subPath;
    m_arcPathList.append(path);
    // 设置圆弧颜色
    QRadialGradient radialGradient;
    radialGradient.setCenter(0, 0);
    radialGradient.setRadius(m_radius);
    radialGradient.setColorAt(0, color);
    radialGradient.setColorAt(1.0, color);
    m_colorList.append(radialGradient);
}

void CustomBox::mousePressEvent(QMouseEvent *event)
{
    QPoint mousePressPoint = event->pos();
    QPoint translatePoint = mousePressPoint - QPoint(width() >> 1, height() >> 1);
//    qDebug()<<"translatePoint x:"<<translatePoint.x()<<"translatePoint y:"<<translatePoint.y();
    if(abs(translatePoint.x())>30 || abs(translatePoint.y())>30){
         ControlMode = 2;//固定模式
         if(abs(translatePoint.y())<100){
            emit signalBoxSlider(true);
         }
         if((translatePoint.y()<-70 && translatePoint.y()>-100) &&
            (translatePoint.x()>-15 && translatePoint.x()<15)){
            mCurWorkRegion = QUADRANT_UP;
//            qDebug("QUADRANT_UP");
         }else if((translatePoint.y()>70 && translatePoint.y()<100) &&
            (translatePoint.x()>-15 && translatePoint.x()<15)){
            mCurWorkRegion = QUADRANT_DOWN;
//            qDebug("QUADRANT_DOWN");
         }else if((translatePoint.y()>-15 && translatePoint.y()<15) &&
            (translatePoint.x()>70 && translatePoint.x()<100)){
            mCurWorkRegion = QUADRANT_RIGHT;
//            qDebug("QUADRANT_RIGHT");
         }else if((translatePoint.y()>-15 && translatePoint.y()<15) &&
            (translatePoint.x()<-70 && translatePoint.x()>-100)){
            mCurWorkRegion = QUADRANT_LEFT;
//            qDebug("QUADRANT_LEFT");
         }else if((translatePoint.y()<-30 && translatePoint.y()>-80) &&
            (translatePoint.x()>30 && translatePoint.x()<80)){
            mCurWorkRegion = QUADRANT_UP_RIGHT;
//            qDebug("QUADRANT_UP_RIGHT");
         }else if((translatePoint.y()<-30 && translatePoint.y()>-80) &&
            (translatePoint.x()<-30 && translatePoint.x()>-80)){
            mCurWorkRegion = QUADRANT_UP_LEFT;
//            qDebug("QUADRANT_UP_LEFT");
         }else if((translatePoint.y()>30 && translatePoint.y()<80) &&
            (translatePoint.x()>30 && translatePoint.x()<80)){
            mCurWorkRegion = QUADRANT_DOWN_RIGHT;
//            qDebug("QUADRANT_DOWN_RIGHT");
         }else if((translatePoint.y()>30 && translatePoint.y()<80) &&
            (translatePoint.x()<-30 && translatePoint.x()>-80)){
            mCurWorkRegion = QUADRANT_DOWN_LEFT;
//            qDebug("QUADRANT_DOWN_LEFT");
         }else{
             emit signalBoxSlider(false);
             mCurWorkRegion = QUADRANT_NO_DEFINE;
         }
         SetSliderValueFun(SliderValue_t);
    }else{
         ControlMode = 1;//自由模式
         emit signalBoxSlider(false);
         mCurWorkRegion = QUADRANT_NO_DEFINE;
    }
    for (int i = 0; i < m_arcPathList.count(); i++)
    {
        if (m_arcPathList[i].contains(translatePoint))
        {
            m_pressIndex = i;
            m_isMousePressed = true;
            update();
            emit signalButtonClicked(i);
            break;
        }
    }
}

void CustomBox::mouseReleaseEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    if (m_isMousePressed)
    {
        m_isMousePressed = false;
        mCenterRound = QPoint(0,0);
        mDegreePixmap_x = QPixmap(0,0);
        mDegreePixmap_y = QPixmap(0,0);
        if(ControlMode ==1){
            ControlMode=0;
        }
        objPointValue.setX(0);
        objPointValue.setY(0);
        if(ControlMode !=2){
            BoxData gB_QVar;
            QVariant eB_QVar;
            gB_QVar.BoxValueX = 0;
            gB_QVar.BoxValueY = 0;
            gB_QVar.BoxValueZ = 0;
            eB_QVar.setValue(gB_QVar);
            emit (signalBoxVelToUi(eB_QVar));
        }
        emit signalButtonReleased(m_pressIndex);
        update();
    }
}

void CustomBox::mouseMoveEvent(QMouseEvent *event)
{
    if(m_isMousePressed){
        mCenterRound = event->pos() - QPoint(width() >> 1, height() >> 1);
        if(mCenterRound.x()>m_radius)mCenterRound.setX(m_radius);
        if(mCenterRound.x()<-m_radius)mCenterRound.setX(-m_radius);
        if(mCenterRound.y()>m_radius)mCenterRound.setY(m_radius);
        if(mCenterRound.y()<-m_radius)mCenterRound.setY(-m_radius);
        objPointValue.setX(mCenterRound.x());
        objPointValue.setY(-mCenterRound.y());
        calcVel_xValue = (double)((-mCenterRound.y()*vel_x_MaxValue)/100.f);
        calcAng_yValue = (double)(mCenterRound.x()*ang_z_MaxValue/100.f);
        mDegreePixmap_x = getPixmap(abs(mCenterRound.x()));
        mDegreePixmap_y = getPixmap(abs(mCenterRound.y()));
        showVel_xValue = QString("").sprintf("%.2lf",fabs(calcAng_yValue));
        showAng_yValue = QString("").sprintf("%.2lf",fabs(calcVel_xValue));
//        qDebug("mCenterRound.x():%d mCenterRound.y():%d",abs(mCenterRound.x()),abs(mCenterRound.y()));
        update();
    }
}
/*************************************************
Function:       analysisAngle
Description:    获取当前鼠标的度数
Input:          x,y
Return:         int,当前鼠标的度数
*************************************************/
int CustomBox::analysisAngle(int x,int y)
{
    double angle;
    if(x == 0 && y > 0){
        angle =  90;
    }else if(x == 0 && y < 0){
        angle = 270;
    }
    else if(x >= 0 && y == 0){
        angle =  0;
    }
    else if(x < 0 && y == 0){
        angle = 180;
    }else{
        angle = atan2(qAbs(y),qAbs(x))/(2*acos(-1))*360;
        if(x < 0 && y > 0){
            angle = 180 - angle;
        }
        if(x < 0 && y < 0){
            angle += 180;
        }
        if(x > 0 && y< 0){
            angle = 360 - angle;
        }
    }
    return angle;
}

QPixmap CustomBox::getPixmap(const int ping)
{
    return getSignalPixmap(QColor(0x5,0x00c7,0xc7),getLineNum(ping));
}

QColor CustomBox::getColor(const int ping )
{
    if(ping<=10)
        return QColor(0xea,0x00,0x00);
    else if(ping<=20)
        return QColor(0xff,0x00,0x80);
    else if(ping<=30)
        return QColor(0xe8,0x00,0xe8);
    else if(ping<=40)
        return QColor(0xea,0xc1,0x00);
    else if(ping<=50)
        return QColor(0xe1,0xe1,0x00);
    else if(ping<=60)
        return QColor(0x9a,0xff,0x02);
    else if(ping<=70)
        return QColor(0x00,0xff,0xff);
    else if(ping<=80)
        return QColor(0x28,0x94,0xff);
    else
        return QColor(0x6a,0x6a,0xff);
}

int CustomBox::getLineNum(const int ping)
{
    if(ping<=20)
        return 1;
    else if(ping<=40)
        return 2;
    else if(ping<=60)
        return 3;
    else if(ping<=80)
        return 4;
    else
        return 5;
}

QPixmap CustomBox::getSignalPixmap(const QColor &color,const int linenum)//获取信号位图
{
    QPixmap pixmap(60,30);
    pixmap.fill(QColor(255,255,255,0));
    QPainter painter(&pixmap);
    painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
    painter.setPen(QPen(color,2,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin));

    //30 - i* 5 ,是为了顶部不要被削掉
    for(int i=1,xpos=0;i<=linenum;++i,++xpos)
    {
        painter.drawArc(30 - i * 6, 30 - i * 5, i * 12, i * 10, 53 * 16, 37 * 2 * 16);
    }
    return pixmap;
}

void CustomBox::getSliderValueSlots(float value){
    switch(ControlMode){
        case 2:{
            SliderValue_t = value;
            if(!boxSendTimer->isActive()){
                boxSendTimer->start(100);
            }
        }break;
        default:
         break;
    }
}
void CustomBox::SetSliderValueFun(float value){
//    qDebug("SetSliderValueFun");
    BoxData gB_QVar;
    QVariant eB_QVar;
    switch(mCurWorkRegion){
        case QUADRANT_UP:{
            gB_QVar.BoxValueX = value;
            gB_QVar.BoxValueY = 0;
            gB_QVar.BoxValueZ = 0;
        }break;
        case QUADRANT_UP_LEFT:{
            gB_QVar.BoxValueX = value;
            gB_QVar.BoxValueY = 0;
            gB_QVar.BoxValueZ = value;
        }break;
        case QUADRANT_LEFT:{
            gB_QVar.BoxValueX = 0;
            gB_QVar.BoxValueY = 0;
            gB_QVar.BoxValueZ = value;
        }break;
        case QUADRANT_DOWN_LEFT:{
            gB_QVar.BoxValueX = -value;
            gB_QVar.BoxValueY = 0;
            gB_QVar.BoxValueZ = value;
        }break;
        case QUADRANT_DOWN:{
            gB_QVar.BoxValueX = -value;
            gB_QVar.BoxValueY = 0;
            gB_QVar.BoxValueZ = 0;
        }break;
        case QUADRANT_DOWN_RIGHT:{
            gB_QVar.BoxValueX = -value;
            gB_QVar.BoxValueY = 0;
            gB_QVar.BoxValueZ = -value;
        }break;
        case QUADRANT_RIGHT:{
            gB_QVar.BoxValueX = 0;
            gB_QVar.BoxValueY = 0;
            gB_QVar.BoxValueZ = -value;

        }break;
        case QUADRANT_UP_RIGHT:{
            gB_QVar.BoxValueX = value;
            gB_QVar.BoxValueY = 0;
            gB_QVar.BoxValueZ = -value;
        }break;
        case QUADRANT_NO_DEFINE:{
            gB_QVar.BoxValueX = 0;
            gB_QVar.BoxValueY = 0;
            gB_QVar.BoxValueZ = 0;
        }break;
    }
    eB_QVar.setValue(gB_QVar);
    emit (signalBoxVelToUi(eB_QVar));
}
void CustomBox::boxSendTimer_clicked()
{
    switch (ControlMode) {
    //自由模式
    case 1:{
        BoxData gB_QVar;
        QVariant eB_QVar;
        gB_QVar.BoxValueX = calcVel_xValue;
        gB_QVar.BoxValueY = 0;
        gB_QVar.BoxValueZ = -calcAng_yValue;
        eB_QVar.setValue(gB_QVar);
        emit (signalBoxVelToUi(eB_QVar));
    }break;
    //固定模式
    case 2:
        SetSliderValueFun(SliderValue_t);
    default:
        if(boxSendTimer->isActive()){
            boxSendTimer->stop();
        }
        break;
    }
}
void CustomBox::vel_x_MaxSlots(double value){
    vel_x_MaxValue = value;
}
void CustomBox::ang_z_MaxSlots(double value){
    ang_z_MaxValue = value;
}

第四步:在UI窗口对手柄界面进行提升为CustomBox类。

第五步:在界面逻辑处理部分则是需要添加串口收发功能和逻辑的处理即可完成手柄功能的编写

演示视频链接:https://www.bilibili.com/video/BV17d4y1N7tg/?vd_source=800b2cb231472d4e741784f506df469f文章来源地址https://www.toymoban.com/news/detail-519796.html

到了这里,关于QT实现一个摇杆手柄的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Web控件UI手动摇杆(虚拟手柄)

    演示地址:http://tools.pt.wsing.cn/VirtualRocker/ 源码下载地址:https://download.csdn.net/download/lyp1215/87583275 触摸屏手动摇杆,根据移动距离触发跟踪事件得到参数: 参数 说明 VirtualRockerID 手柄ID evType 状态:开始、移动、停止 deltaX X轴偏移量 deltaY Y轴偏移量 方法: 名称 描述 参数 Ini

    2024年01月18日
    浏览(20)
  • Qt优秀开源项目之二十一:遇见QSkinny,一个轻量级Qt UI库

    目录 一.QSkinny简介 二.工作原理 三.编译 QSkinny库基于Qt Graphic View和Qt/Quick中少量的核心类。它提供了一组轻量级控件,可以在C++或QML中使用这些控件。QSkinny默认是启用硬件加速的,非常适合嵌入式设备,目前已经应用于大量汽车GUI的代码实现中。正如预期的那样,它有良好的

    2024年01月23日
    浏览(63)
  • 【qt】仿写一个智能家居页面

    B站大学 示例图片: 后面的保持默认,点击完成。成功后可以看到: 再将图片拖入工程文件夹下面。 向页面添加图片 前缀名可以是自己设定,注意添加完毕后一定要先保存(ctrl+s),看文件后面的小星号*消失就保存成功,否则后面无法添加文件。 回到ui文件,将一个label拖到

    2024年04月27日
    浏览(37)
  • 【Unity】【Pico】手柄摇杆控制第一人称移动和旋转

    背景:开发影院系统 环境:Unity2021.3、PicoNeo3ProEye 描述 :已经在Unity项目中实现第一人称WASD移动和鼠标旋转(代码见我的其他博文) 需求 :希望项目在Pico一体机运行时可以通过手柄摇杆控制(unityxr的基础配置见我的其他博文) 解决 : Locomotion System XR Origin Continuous Move Pr

    2024年01月18日
    浏览(91)
  • CMake引用QT、CMake构建一个转换为3d tile的开源代码

    在CMake里单独运行一下  find_package(Qt5     REQUIRED    COMPONENTS Core Xml Test) ,Core Xml Test 这三个是需要的qt组件; 情况如下;提示找不到QT; 根据资料,cmake引用qt需要3-4个方面,     首先Qt包含三个编译工具:moc、uic、rcc, moc:元对象编译器(Meta Object Compiler),用来处理带有

    2024年02月05日
    浏览(38)
  • python获取ps4手柄信息并使用QT界面显示读取内容

    本文使用ps4手柄旨在控制机器人行走,故只获取了手柄的部分信息。 硬件:PS4手柄 环境:win7/10,python3.7,opencv4.0,Pyqt5,pygame,time,math,sys 主要代码如下: 使用QT Designer绘制好界面,然后保存为.ui文件,最后将.ui生成.py文件导入程序中即可使用。 QT Designer软件界面如下: 使用命令

    2023年04月20日
    浏览(49)
  • 使用U3D、pico开发VR(二)——添加手柄摇杆控制移动

    1System: 2Move Speed:注意速度过小会导致看起来没有移动 3RightHandMoveAction(right loco move):个人采用右手柄实现移动 1System: 2Turn Speed:注意旋转速度过小会导致看起来没有移动 3RightHandMoveAction:个人采用右手柄实现旋转 至此,初步的移动功能就实现了

    2024年02月08日
    浏览(67)
  • 个人实现的QT拼图游戏(开源),QT拖拽事件详解

    效果图 引言 在学习QT demo时,发现有一个拼图demo,介绍拖拽事件的。以此为蓝本加了 亿点 修饰,就诞生了这个游戏。 玩法 游戏为拼图游戏,分为俩种模式(闯关与休闲)。 闯关模式:在规定的时间内完成拼图,共有四关,有三种难度,每种难度所需的时间不一致。 休闲模

    2024年01月20日
    浏览(46)
  • 基于ROS和YOLO的SLAM智能小车仿真系统设计

    Ubuntu 20.4 ros-noetic gazebo yolov4 nvidia525+cuda10.1+cudnn_7.6.5 将darknet文件夹移动到darknet_ros文件夹下 链接: https://download.csdn.net/download/qq_42281475/87502982. 将下载的yolo_network_config替换roscar_gazebo_yolov4/src/darknet_ros/darknet_ros/目录下的文件 链接: https://download.csdn.net/download/qq_42281475/87502991

    2024年02月10日
    浏览(40)
  • 开源!手把手教你搭建Arduino+英伟达Jetson的ROS小车(下)

    想起去年元旦收到群里面小伙伴儿的建议,希望我们也能够出一个基于NVIDIA Jetson nano 的ROS小车搭建过程,于是我们就慢慢书写了前面已经发布的推文集:     《开源!手把手教你搭建Arduino+英伟达Jetson的ROS小车(上)》中,我们介绍了一台Jetson nano小车所需要的硬件部分;

    2024年02月07日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包