View Full Version : Paint event does not Rescale with label maximise

21st June 2013, 08:52
Using qt Creator

Created window with label
Uploads image to label
I set the the label to "expanding" so its size increases as window maximizes
The image is set to "scaled" -> also increases with label

Using QPainter I am able to draw points on image & lines between them.
The problem is Qpainter does not increase as image expands, Hence my
points then are not placed in the correct location where initially clicked.

Is there a Setting to change to scale the QPainter?
Or is there another work around it

Ideas most welcome :)

Kind Regards

21st June 2013, 09:14
QPainter has a scale method.


21st June 2013, 10:03

1)How do I get it to scale with Window size
QPainter::scale(qreal sx, qreal sy) ???

2) Do Iplace the code in constructor or in the paintevent

Kind Regards
Example code, pls if possible:)

21st June 2013, 10:22
You can use resizeEvent and capture the resized rect of label. and set your painter according to that.

21st June 2013, 12:10
Not winning :confused:

Created in header class of myLabel (as QPainter is within this class)

void resizeEvent(QResizeEvent *event);

implementation.cpp: :confused::confused:

void resizeEvent(QResizeEvent *event)
//Taken partially from example Scribble ( http://www.trinitydesktop.org/docs/qt4/widgets-scribble.html )

if(width() > fplabel.width() || height() > fplabel.height())
int newWidth = qMax(width()+128, fplabel.width());
int newHeight = qMax(height()+128, fplabel.height());
QSize(newWidth, newHeight);
QImage newImage(newSize);
QPainter painter(&newImage);
painter.drawImage(QPoint(0, 0), *image);
*image = newImage;

Image is uploaded in main class

Not sure how to go about it

ideas please

21st June 2013, 12:48
Save the width and height of the label from resizeEvent and call update, reimplement the paint event and paint on label.....

also scale the QImage according to the width and height before paint, but do not change the original image.


Santosh Reddy
21st June 2013, 12:54
You don't need resizeEvent().

All you need is to scale QPainter, also scale the points and lines you store. Here is an working example. I used some of your code from other thread, I hope you recollect :)

class Label : public QLabel
QList<QPoint> current_pos;
QSize size;

explicit Label(QWidget * parent = 0);

void mouseMoveEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);

Label::Label(QWidget * parent)
: QLabel(parent)
, size(100, 100)
QRect rect;
rect.setTopLeft(QPoint(100, 100));

void Label::paintEvent(QPaintEvent *event)

QPainter painter(this);

qreal sx = event->rect().width() / (qreal)size.width();
qreal sy = event->rect().height() / (qreal)size.height();

painter.scale(sx, sy); // scale the painter

QPen pen(Qt::green, 4, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);

foreach(QPoint point, current_pos)

void Label::mouseMoveEvent(QMouseEvent *event)
qreal sx = rect().width() / (qreal)size.width();
qreal sy = rect().height() / (qreal)size.height();

QPoint p;
p.setX(event->pos().x() / sx); // Scale to original before storing
p.setY(event->pos().y() / sy);

current_pos << p;

int main(int argc, char ** argv)
QApplication app(argc, argv);

Label l;

return app.exec();

21st June 2013, 14:25
(@Santosh -> remembered older thread -> fantastic:D)
Thanks That code works, partially though

new problems arise as code is implemented
initial line before altering window size is displayed & now scaled accordingly
however now other functionality that used to work doesnt
-> Image upload, no longer places image in label // used to work basically user presses btnUpload, image is set to label (mainClass)
-> line is drawn points are not shown(tested without uploading image) // before added the scaling code, points were shown(paint event)
-> if I draw a line and then maximise line is scaled, but no future lines are shown!

Any Ideas?
Thanks for the help @All &
Thanks for the welcoming assistance @Santosh Reddy

Santosh Reddy
21st June 2013, 14:38
At this point I think you know every thing that is required to make it work. Its just matter of figuring out how to put them togeather.

BTW where are you painiting, on QLabel or QMainWindow?

21st June 2013, 15:05
Painting in QLabel

Will try figuring out how to put them together.
Thanks for all your assistance

If any ideas(road map) for this problem come to mind
Give me a shout.

#define Santosh
ValuableInput = true;

Kind Regards

24th June 2013, 08:42
Good Day, 1nAll

K Thanks to your help, I got most of my re-scaling and painting Event working.

What I cant get right.
Once I maximise my window, event re-scales
But when I create a new point/line it does not start where my mouse pointer is!
The point is drawn way RIGHT of where the actual mouse is.

Mouse Pointer (*) -----------------------------> Point Starts Way of the marker
Any Ideas
Kind Regards

Getting to grips with qt -> Awesome qt

25th June 2013, 09:08
How do I Save the width and height of the label from resizeEvent
& reimplement the paint event and paint on label.....
& also scale the QImage according to the width and height before paint,

Example please

26th June 2013, 05:14
You can use a local QRect variable which will store the geometry of label on each and every resize event (Well we only need width and height).
In resize event you can call update() function for the label.
and yah for scaling image.... you can use the scale function of QImage class.

well you can do this all via another way... forget everything like resize event , update, and etc..,.,. You can use setPixmap method of QLabel..
this will do everything like rezise, repaint everything you need.... try both techniques.... this will help you most..