Important: Please read the Qt Code of Conduct -

[SOLVED] Resize Paintevent (fit to widget)

  • I draw two rects like this.
    Its working but I want the rects to scale when my widget is getting bigger and shrink when my widget is getting smaller.

    How can I do this.

    void Lines::paintEvent(QPaintEvent *e)
    QPainter qp(this);
    QPen pen (Qt::blue, 2, Qt::SolidLine);

    QPainter pp(this);
    QPen pen2(Qt::green, 2, Qt::SolidLine);

    Whats the best way to do this?


  • I think sizes of your rects should refers to size of widget, for example:
    const int currentWidth = width();
    const int currentHeight = height();
    qp.drawRect(currentWidth * 0.1, currentHeight * 0.1, currentWidth * 0.5, currentHeight * 0.5);

  • yai! its working, thanks,

    can you also provide a little function for example like this

    if widget is resized

    {do soemthing...}

    € No need anymore, I didnt know, it could be any variable, so I simply created currentWidth and currentWidth2
    and making a compare (currentWidth2!=currentWidth)

    gonna mark it as solved

Log in to reply