Please nominate your Qt Champions for 2021!

QPainter not drawing the rectangle on QLabel

  • I am displaying image on q QLabel and want to draw a rectangle on it with mouse movement

    Image is loaded correctly and selectionRect has correct co-ordinates. Its co-ordinates are changing with mouse move.

    However, This painEvent is not painting. Could someone help me on this?

    void XWizardPage :: paintEvent(QPaintEvent * e)
    QImage tmp(label1->pixmap()->toImage());
    QPainter painter(&tmp);
    QPen paintpen(Qt::red);



    Secondly, How can I draw multiple rectangles on my QLabel?

  • Found the mistake, I was not setting image back to the label.

    Added the following line and it works more than perfect.
    @ label1->setPixmap(QPixmap::fromImage(tmp));@

    How can I avoid the "intermediate drawings":
    ! image)!

  • [quote author="tesmai4" date="1406043994"]How can I avoid the "intermediate drawings":
    ! image)![/quote]

    I guess you need to redraw the part of image that is covered by previous rectangle and then draw your new rectangle.

    BTW(By The Way) If you trying to implement a selection rectangle then take a look on "QRubberBand":

Log in to reply