PDA

View Full Version : Paint Event does not correspond to coordinates!



ebsaith
25th June 2013, 08:06
Good Day,

I'm struggling with resizing my paint event!:(:confused:

In my constructor:


qLabel::qLabel(QWidget *parent) :
QLabel(parent), windowSize(333, 494)
{...
QRect rect;
rect.setTopLeft(QPoint(9, 9));
rect.setSize(windowSize);
setGeometry(rect);
}


In my paint event


void mmetLabel::paintEvent(QPaintEvent *ev)
{
QLabel::paintEvent(ev);
QPainter painter(this);

qreal sx = ev->rect().width() / (qreal)windowSize.width();
qreal sy = ev->rect().height() / (qreal)windowSize.height();
painter.scale(sx, sy);
...
//draw points and lines between every 2 consective points
}


Once I click on my label, I display the coordinates of the mouse ->working
Label also displays the 1st point and 2nd point coordinates -> working

My Paintevent -> NOT Working accordingly

It draws points not where mouse is clicked, hence the lines are completely not right
example, If I click at point1 (x=100, y=100) & click point2(x=152, y=251)
it draws a line from (x=230, y=128) -> (x=351, y=195) = completely wrong!

Please help
Kind Regards
Previous Thread(Title: Re: Paint event does not Rescale with label maximise)

anda_skoa
25th June 2013, 10:13
Hmm, that looks wrong. I don't think you should be taking the event's rect, that can be only part of the widget.

If I understand your goal correctly, then you might be better off by transforming your points when the widget resizes.

That way your points are always in the same frame of reference as the widget itself.

Cheers,
_

ebsaith
25th June 2013, 10:29
Thanks,
Transforming points? how do i go about it

wysota
25th June 2013, 10:41
What is "windowSize"? Where do you get that value from?

ebsaith
25th June 2013, 11:29
I set my windowSize in constructor

in my class:
QSize windowSize;

The label I created is of that size... under forms, I click on label, it shows the size in the right toolBar(property/value)



mmetLabel::mmetLabel(QWidget *parent) :
QLabel(parent), windowSize(333, 494)
{
QRect rect;
rect.setTopLeft(QPoint(9, 9));
rect.setSize(windowSize);
setGeometry(rect);
}

Added after 31 minutes:

Re: moving around code with flags

[code]
void resizeEvent(QResizeEvent *ev)
{
wresize = true;
}


in paintEvent


if(wresize == true)
{
qreal sx = (ev->rect().width() / (qreal)windowSize.width());
qreal sy = (ev->rect().height() / (qreal)windowSize.height());
painter.scale(sx, sy);
wresize = false;
}


How do I set a flag in ResizeEvent... It gives me an error when I try to set the flag to true in ResizeEvent
Any Ideas, Not sure if this implementation will work though... testing ideas

wysota
25th June 2013, 11:35
But what is the purpose of all that code? You can get the label width and height by calling width() and height(), you don't need any "windowSize" for that. ev->rect().width() is going to return the width of the exposed rect which can be anything ranging from 1 to the width of your label. Hence your code makes no sense. Your drawing code needs to draw within QRect(0,0, width(), height()).

ebsaith
25th June 2013, 11:44
Please give me a simple example on how to resize my paintEvent
So when a user clicks a point it displays a point(working)
& when window is resized that point is adjusted accordingly

I'm stuck, tried many plausible theories to no avail though
Assistance please

example code appreciated

wysota
25th June 2013, 13:06
I have no idea what you mean by "resize my paintEvent".

Resizing the window should not influence your drawing code in any way. Your canvas is simply larger. If you mean that you wish to scale the contents then use QPainter::setWindow(), QPainter::setViewport() and QPainter::worldTransform() (the latter can be used to map mouse event coordinates to what you specify by setWindow()).

The most trivial approach would be something along:


void X::mousePressEvent(QMouseEvent *me) {
QPointF meF = me->pos();
m_points << QPointF(100*meF.x()/(qreal)width(), 100*meF.y()/(qreal)height());
update();
}

void X::paintEvent(QPaintEvent *pe) {
QPainter p(this);
p.setViewport(rect());
p.setWindow(QRect(0,0,100,100));
foreach(QPointF pt, m_points) p.drawPoint(pt);
}

void X::resizeEvent(QResizeEvent*) { update(); }

ebsaith
25th June 2013, 13:25
Thanks will try it out

My apologies, What I meant by "resize my paintEvent".
Is If I draw a point(p1) at lets say (x=50, y=50)
& I resize my window to lets say double the initial size of the window
that The p1 should now move to (x=100, y=100)
The painted point needs to move with the resize!

further Ideas welcome
Kind Regards

wysota
25th June 2013, 13:36
What I gave you earlier should work fine. Another approach, one that doesn't use setWindow would be:


void X::mousePressEvent(QMouseEvent *me) {
m_pts << QPointF(me->pos().x()/(qreal)width(), me->pos().y()/(qreal)height());
}

void X::paintEvent(QPaintEvent *) {
QPainter p(this):
foreach(QPointF pt, m_pts) p.drawPoint(pt.x()*width(), pt.y()*height());
}

ebsaith
26th June 2013, 08:29
Thanks @ wysota

I got one snag with the code!
My code draws the line between 2 mouse clicks(2 points) ---> line is shown
But the points are not displayed * *//though
If I dont use the resize code (testing) resize points are shown!
Any ideas, where the problem may be

Kind Regards

wysota
26th June 2013, 08:33
Show your code.

anda_skoa
26th June 2013, 09:24
Transforming points? how do i go about it

You create a QTransform object, apply all necessary transformations to it (e.g. scale), then iterate over the points and have the transform object map them to their new value which you then store again.

Cheers,
_

ebsaith
26th June 2013, 09:46
When I try using painter.drawPoint(p1); doesnt work
Tried


painter.drawEllipse(TempMark, 2, 2);

works fine!
For the time being will use ellipse.

With regards to topic:
Re: Paint Event does not correspond to coordinates! SOLVED!
Many Thanks to all
;)

wysota
26th June 2013, 10:17
When I try using painter.drawPoint(p1); doesnt work
It works but it draws a small point.