PDA

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



ebsaith
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
#How?

Ideas most welcome :)

Thanks
Kind Regards

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

Cheers,
_

ebsaith
21st June 2013, 10:03
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:)

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

ebsaith
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;
update();
}
}


Image is uploaded in main class

Not sure how to go about it

ideas please

karankumar1609
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.


THATS SO SIMPLE....
CHEERS

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;

public:
explicit Label(QWidget * parent = 0);

protected:
void mouseMoveEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
};

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

void Label::paintEvent(QPaintEvent *event)
{
QLabel::paintEvent(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);
painter.setPen(pen);

foreach(QPoint point, current_pos)
painter.drawPoint(point);
}

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;
this->repaint();
}

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

Label l;
l.show();

return app.exec();
}

ebsaith
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?

ebsaith
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

ebsaith
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

ebsaith
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
@karankumar1609

karankumar1609
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..


CHEERS.....