Paint Event does not correspond to coordinates!
Good Day,
I'm struggling with resizing my paint event!:(:confused:
In my constructor:
Code:
QLabel(parent
), windowSize
(333,
494) {...
rect.
setTopLeft(QPoint(9,
9));
rect.setSize(windowSize);
setGeometry(rect);
}
In my paint event
Code:
{
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)
Re: Paint Event does not correspond to coordinates!
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,
_
Re: Paint Event does not correspond to coordinates!
Thanks,
Transforming points? how do i go about it
Re: Paint Event does not correspond to coordinates!
What is "windowSize"? Where do you get that value from?
Re: Paint Event does not correspond to coordinates!
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)
[code]
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
in paintEvent
Code:
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
Re: Paint Event does not correspond to coordinates!
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()).
Re: Paint Event does not correspond to coordinates!
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
Re: Paint Event does not correspond to coordinates!
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:
Code:
m_points <<
QPointF(100*meF.
x()/(qreal
)width
(),
100*meF.
y()/(qreal
)height
());
update();
}
p.setViewport(rect());
p.
setWindow(QRect(0,
0,
100,
100));
foreach
(QPointF pt, m_points
) p.
drawPoint(pt
);
}
Re: Paint Event does not correspond to coordinates!
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
Re: Paint Event does not correspond to coordinates!
What I gave you earlier should work fine. Another approach, one that doesn't use setWindow would be:
Code:
m_pts <<
QPointF(me
->pos
().
x()/(qreal
)width
(), me
->pos
().
y()/(qreal
)height
());
}
foreach
(QPointF pt, m_pts
) p.
drawPoint(pt.
x()*width
(), pt.
y()*height
());
}
Re: Paint Event Points does not correspond to coordinates!
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
Re: Paint Event Points does not correspond to coordinates!
Re: Paint Event does not correspond to coordinates!
Quote:
Originally Posted by
ebsaith
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,
_
Re: Paint Event does not correspond to coordinates!
When I try using painter.drawPoint(p1); doesnt work
Tried
Code:
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
;)
Re: Paint Event does not correspond to coordinates!
Quote:
Originally Posted by
ebsaith
When I try using painter.drawPoint(p1); doesnt work
It works but it draws a small point.