Paint event does not Rescale with label maximise
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
#How?
Ideas most welcome :)
Thanks
Kind Regards
Re: Paint event does not Rescale with label maximise
QPainter has a scale method.
Cheers,
_
Re: Paint event does not Rescale with label maximise
Thanks
#How
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:)
Re: Paint event does not Rescale with label maximise
You can use resizeEvent and capture the resized rect of label. and set your painter according to that.
Re: Paint event does not Rescale with label maximise
Not winning :confused:
Created in header class of myLabel (as QPainter is within this class)
implementation.cpp: :confused::confused:
Code:
{
//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
);
painter.
drawImage(QPoint(0,
0),
*image
);
*image = newImage;
update();
}
}
Image is uploaded in main class
Not sure how to go about it
ideas please
Re: Paint event does not Rescale with label maximise
Save the width and height of the label from resizeEvent and call update, reimplement the paint event and paint on label.....
also scale the QImageaccording to the width and height before paint, but do not change the original image.
THATS SO SIMPLE....
CHEERS
Re: Paint event does not Rescale with label maximise
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 :)
Code:
{
QList<QPoint> current_pos;
public:
explicit Label
(QWidget * parent
= 0);
protected:
};
, size(100, 100)
{
rect.
setTopLeft(QPoint(100,
100));
rect.setSize(size);
setGeometry(rect);
}
{
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);
painter.setPen(pen);
foreach
(QPoint point, current_pos
) painter.drawPoint(point);
}
{
qreal sx = rect().width() / (qreal)size.width();
qreal sy = rect().height() / (qreal)size.height();
p.setX(event->pos().x() / sx); // Scale to original before storing
p.setY(event->pos().y() / sy);
current_pos << p;
this->repaint();
}
int main(int argc, char ** argv)
{
Label l;
l.show();
return app.exec();
}
Re: Paint event does not Rescale with label maximise
(@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
Re: Paint event does not Rescale with label maximise
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?
Re: Paint event does not Rescale with label maximise
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
Re: Paint event does not Rescale with label maximise
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
Re: Paint event does not Rescale with label maximise
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
@karankumar1609
Re: Paint event does not Rescale with label maximise
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..
CHEERS.....