QT5.9.3高仿探探雷达扫描控件 扫描+回波

闲来无事,简单做了一个基于QT的仿“探探”雷达扫描控件,效果图如下

 

核心代码

1,绘制扫描

void QTanTanScanning::drawScanning(QPainter *painter)
{
    painter->save();
    QConicalGradient conical(0, 0, arc);

    scannigColor.setAlpha(50);
    conical.setColorAt(0, scannigColor);
    scannigColor.setAlpha(0);
    conical.setColorAt(1, scannigColor);

    QPen pen(conical, 2, Qt::SolidLine, Qt::FlatCap, Qt::MPenJoinStyle);
    painter->setPen(pen);
    painter->setBrush(conical);
    painter->drawPie(QRect(- ringMaxR, - ringMaxR, ringMaxR << 1, ringMaxR << 1), arc * 16, 360 * 16);

    painter->drawArc(QRect(- ringMaxR, - ringMaxR, ringMaxR << 1, ringMaxR << 1), 0, 360 * 16);
    painter->restore();

}

2,绘制扩散圈

void QTanTanScanning::drawRings(QPainter *painter)
{
    painter->save();
    painter->setBrush(Qt::NoBrush);
    QVector::iterator iter = rings.begin();
    while(iter != rings.end())
    {
        struct RingData currRing = *iter;
        int radius = currRing.radius;
        //绘制圆弧方法绘制圆环
        float penWidth = currRing.penWidth;
        QRectF rect(-radius + penWidth / 2, -radius + penWidth / 2, radius * 2 - penWidth, radius * 2 - penWidth);
        //可以自行修改画笔的后三个参数,形成各种各样的效果,例如Qt::FlatCap改为Qt::RoundCap可以产生圆角效果
        ringColor.setAlpha(255 - currRing.alpha);
        QPen pen(ringColor, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MPenJoinStyle);

        //绘制总范围角度圆弧
        pen.setColor(ringColor);
        painter->setPen(pen);
        painter->drawArc(rect, 0 * 16, 360 * 16);
        iter++;
    }

    painter->restore();

}

3,绘制中间图片按钮

void QTanTanScanning::drawCenterRing(QPainter *painter)
{
    painter->save();

    QPixmap pixmap(":/image/kt.jpeg");

    QPainterPath path;
    path.addEllipse(QPoint(0, 0), centerR, centerR);
    painter->setClipPath(path);

    QRect rect(-centerR, -centerR, centerR * 2, centerR * 2);
    painter->drawPixmap(rect, pixmap);

    int penWidth = 3;
    //可以自行修改画笔的后三个参数,形成各种各样的效果,例如Qt::FlatCap改为Qt::RoundCap可以产生圆角效果
    QPen pen(Qt::white, penWidth, Qt::SolidLine, Qt::FlatCap, Qt::MPenJoinStyle);
    painter->setPen(pen);
    painter->drawArc(rect, 0, 360 * 16);

    painter->restore();

}

4,中间按钮动画控制

 QPropertyAnimation *anim1 = new QPropertyAnimation(this, "");
    anim1->setStartValue(40);
    anim1->setEndValue(50);
    anim1->setDuration(300);
    QPropertyAnimation *anim2 = new QPropertyAnimation(this, "");
    anim2->setStartValue(50);
    anim2->setEndValue(30);
    anim2->setDuration(300);
    QPropertyAnimation *anim3 = new QPropertyAnimation(this, "");
    anim3->setStartValue(30);
    anim3->setEndValue(40);
    anim3->setDuration(300);

    animationGroup = new QSequentialAnimationGroup() ;
    animationGroup->addAnimation(anim1);
    animationGroup->addAnimation(anim2);
    animationGroup->addAnimation(anim3);

    connect(anim1, SIGNAL(valueChanged(QVariant)), this, SLOT(updateRadius(QVariant)));
    connect(anim2, SIGNAL(valueChanged(QVariant)), this, SLOT(updateRadius(QVariant)));
    connect(anim3, SIGNAL(valueChanged(QVariant)), this, SLOT(updateRadius(QVariant)));

鼠标事件,判断点击和开始动画

void QTanTanScanning::mousePressEvent(QMouseEvent *event)
{
    if(!m_pressflag)
    {
        QPoint pressedPoint =  event->pos();
        QPointF realPos = pressedPoint - rect().center();

        QMatrix qMatrix;
        int width = this->width();
        int height = this->height();
        int side = qMin(width, height);
        qMatrix.scale(side / 200.0, side / 200.0);
        QRegion mEllipse = QRegion(-centerRingR, -centerRingR, centerRingR * 2, centerRingR * 2, QRegion::Ellipse);
        mEllipse = qMatrix.map(mEllipse);
        if( mEllipse.contains(realPos.toPoint()))
        {

            m_pressflag = true;
            //centerRingR = 50;
            animationGroup->stop();
            update();
        }
    }
}

void QTanTanScanning::mouseReleaseEvent(QMouseEvent* event)
{
    if(m_pressflag)
    {
        m_pressflag = false;
        struct RingData ring;
        ring.radius = centerRingR;
        ring.alpha = 255;
        ring.penWidth = 3;
        rings.push_back(ring);
        //centerRingR = 40;
        animationGroup->start();
        update();
    }
}

扫描和扩散定时动态

void QTanTanScanning::ringsTimerWork()
{
    QVector::iterator iter = rings.begin();
    while(iter != rings.end())
    {
        struct RingData currRing = *iter;

        if(currRing.radius < ringMaxR)
        {
            currRing.radius += ringsRStep;
            currRing.alpha = currRing.radius * (255 / ringMaxR);
            currRing.penWidth = 3.0 - currRing.radius * (3.0 / ringMaxR);
            *iter = currRing;
            iter++;
        }
        else
        {
            rings.erase(iter);
        }
    }

    update();
}

void QTanTanScanning::timerWork()
{
    arc--;
    if (arc == 0)
        arc = 360;
    update();
}

源码文件已上传

https://download.csdn.net/download/octdream/11618191

你可能感兴趣的:(QT,C++,qt,自定义控件,雷达,探探,余晖+回波)