zacharoni16
zacharoni16

Reputation: 307

Paint over top of label, not behind it in Qt

Dial

I am creating a simple gauge in Qt 4.7.4, and everything is working wonderfully. Except for the fact that, for the life of me, I cannot get the dial shape to paint over the text labels when it passes over them. It always paints it behind the label. I am just using a simple drawpolygon() method.

I'm thinking this has something to do about paint events? I am drawing everything inside a QFrame inside a MainWindow. I am using QFrame's paintEvent.

Edit: The QLabels are created on start up with new QLabel(this). They are only created once, and never touched again ( Similar to manually adding them on the Ui with Designer). The drawpolygon() is in the QFrame's Paint event.

"myclass.h"

    class gauge : public QFrame
{
    Q_OBJECT

public:
    explicit gauge(QWidget *parent = 0);
    ~gauge();
    void setValues(int req, int Limit, bool extra=false);

private:

    void drawDial();

protected:
    void paintEvent(QPaintEvent *e);

};

"myclass.cpp"

void gauge::paintEvent(QPaintEvent *e)
{
    Q_UNUSED(e);


     drawDial();
    return;
}


void gauge::drawDial()
{


    QPainter Needle(this);
    Needle.save();
    Needle.setRenderHint(Needle.Antialiasing, true); // Needle was Staggered looking, This will make it smooth
    Needle.translate(centrePt); // Center of Widget
    Needle.drawEllipse(QPoint(0,0),10,10);
    Needle.restore();
    Needle.end();
}

Upvotes: 0

Views: 1609

Answers (1)

Jeremy Friesner
Jeremy Friesner

Reputation: 73039

If the gauge widget and the QLabels are siblings, then you can move the gauge widget to the front by calling its raise() method.

If the QLabels are children of the gauge widget, on the other hand, then they will always display in front of it. In that case you can either reorganize your widget hierarchy so that they are siblings instead, or you can get rid of the QLabels and simply call drawText() from your paintEvent() method instead (after drawDial() returns)

Upvotes: 2

Related Questions