PDA

View Full Version : Problem with window-viewport coordinate



damon_1990
14th September 2011, 14:09
Hi. I have a problem with window-viewport coordinates. In my code I draw a polygon:


static const int upPoints_p[4][2] = {
{ 49, -49 }, { -49, -49 },
{ -10, -10 }, { 10, -10 }
};

QPolygon up_p(4, &upPoints_p[0][0]);
painter.setWindow( -50, -50, 100, 100);
painter.drawPolygon(up_p);


and next, I would like to check mousePressEvent:


QPoint point = event->pos();
if( up_p.containsPoint(point, Qt::OddEvenFill) )
{

}

but I have different coordinate. Can someone help me fix my code?

pkj
14th September 2011, 15:18
The event->pos() gives you co-ordinates in the widget world co-ordinates. You perhaps want co-ordinates of mouse click in way you have set the window.
After having done setWindow() call painter.transform().inverted().map(event->pos()) in paint function. This will give you the co-ordinates in your world.

damon_1990
14th September 2011, 16:21
Thanks for answer. So, I change my code:


void Diagram::mousePressEvent(QMouseEvent *event)
{
QPoint point = event->pos();

QPainter painter(&pixmap);
painter.setWindow(-50, -50, 100, 100);

QPoint out = painter.transform().inverted().map(event->pos());

qDebug() << point.x() << point.y();
qDebug() << out.x() << out.y();
}

but I still get the same value, for example:


188 158
188 158

and I can't check this condition


up_p.containsPoint(out, Qt::OddEvenFill)

because I have polygons defiened like:


static const int upPoints_p[4][2] = {
{ 49, -49 }, { -49, -49 },
{ -10, -10 }, { 10, -10 }
};

So, what am I doing wrong?

pkj
14th September 2011, 17:38
Hmm.. you are trying to use QPainter for which device in mousePressevent function handler? You must have also got some message like painter not active. It is because when you are trying to access qpainter in mousepressevent, painter doesn't scale in there. hence worldTransform matrix is identity. If you store the point in some variable in mousepressevent function handler, update the widget, and print coords in paint function you will get the right answer.
If you want to store the painter use a pointer to qpainter and use that object.

Added after 12 minutes:

You can also store worldtransformationmatrix given by worldTransform() and use it map back to logical coordinates in mousepressevent.

damon_1990
14th September 2011, 18:19
Thanks for answer. I'm not sure I did this correctly, but this not working for me.


#include <QWidget>

class Test : public QWidget
{
Q_OBJECT
public:
explicit Test(QWidget *parent = 0);

protected:
void paintEvent(QPaintEvent *);
void mousePressEvent(QMouseEvent *);

private:
QPoint f;

};



#include <QtGui>
#include "test.h"

Test::Test(QWidget *parent) :
QWidget(parent){ }

void Test::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setWindow(-50, -50, 100, 100);

qDebug() << f.x() << " " << f.y();

QPoint point = painter.transform().inverted().map(f);
qDebug() << point.x() << " " << point.y();

}

void Test::mousePressEvent(QMouseEvent *event)
{
f = event->pos();
update();
}

Does anyone know what is wrong with my code?

pkj
14th September 2011, 18:31
Use painter.deviceTransform().inverted().map(f);

damon_1990
15th September 2011, 13:17
Yes, it works! Thanks a lot!