Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Weird drawPath problem



  • So I have this problem: I designed a gauge that works almost always. In some environments the indicator is not shown. In the picture, the top gauge is correct, the bottom is wrong.

    I attach the code that draws the indicator (needle).

    In debug mode is working (of course...). In release, sometimes works, other times does not. Even in the same window, as in the picture.

    Changing execution mode to Win 7 compatibility makes it work.
    Executing with admin privilege makes it work.

    0_1559731304505_2b8d7a04-54e0-4f30-9911-d6d4de196c92-image.png ![alt text](image url)

    void Gauge::drawNeedleShort (QColor color)
    {
        QPainter p(this);
        p.setRenderHint(QPainter::Antialiasing);
    
        const float nedR = (extR+intR)/2;
        const float nR = intR*0.9;
        const float ncA = 4;
    
        QPainterPath needle;
        needle.moveTo(cX+nedR*cosfD(mAngle),cY-nedR*sinfD(mAngle));
    
        needle.lineTo(cX+nR*cosfD(mAngle+ncA),cY-nR*sinfD(mAngle+ncA));
        needle.arcTo(cX-nR,cY-nR,2*nR,2*nR,mAngle+ncA,-2*ncA);
        needle.closeSubpath();
    
        p.setPen(color);
        p.setBrush(QBrush(color));
        p.drawPath(needle);
    }
    


  • Comes out there was an uninitialized variable that prevent drawing. It seems that IDE always initializes memory with some value.


  • Lifetime Qt Champion

    How do you call drawNeedleShort()? Hopefully only within the paintEvent().



  • This post is deleted!


  • @Christian-Ehrlicher said in Weird drawPath problem:

    How do you call drawNeedleShort()? Hopefully only within the paintEvent().

    Exactly, here it is the paintEvent. As you can note also the drawRange is not working in the bottom picture: the green sector below the indicator is not shown. drawQuadrant and drawTicks seem work every time.

    void Gauge::paintEvent(QPaintEvent *event)
    {
        drawQuadrant();
        QColor drawColor=QWidget::palette().color(QWidget::foregroundRole());
        drawRange();
        drawNeedleShort(drawColor);
        drawTicks();
    }
    
    


  • Comes out there was an uninitialized variable that prevent drawing. It seems that IDE always initializes memory with some value.


  • Lifetime Qt Champion

    @Landolfi said in Weird drawPath problem:

    Comes out there was an uninitialized variable that prevent drawing. It seems that IDE always initializes memory with some value.

    Hi,

    No, the IDE as nothing to do with that. If you don't explicitly initialize your variables with a value, they will contain garbage that may or may not have any sense.


Log in to reply