[Qt-interest] mouse and polygons
Malyushytsky, Alex
alex at wai.com
Wed Nov 17 20:24:23 CET 2010
The easiest way to find why you get segment fault usually is to use debugger.
At a glance you have unsafe implementation of addPolygons()
which assumes QVector<QPointF> &points has at least 4 points.
Similar assumptions are made in other place.
With C++ you should be careful with your assumptions.
Also I would suggest you to review documentation on function before you are using them.
For example:
removeItem(tempItem) will not delete item, as I think you assume each time you do it you create memory leak.
As for keeping mouse inside polygon - you can't. User is able to move mouse where he wants.
What you can do is to change way you treat the movement.
For example you could register that user started movement on mount press, collect information you need about item in temporary storage and use it until you need it (in mouseMove for example).
Alex
From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Mahendra G.R
Sent: Wednesday, November 17, 2010 1:25 AM
To: qt-interest at trolltech.com
Subject: Re: [Qt-interest] mouse and polygons
Hi all,
anybody?, i just want to know how to contain the mouse pointer within the polygon, or avoid seg fault when mouse pointer goes out of my polygon,
Regards,
On Fri, Nov 12, 2010 at 5:01 PM, Mahendra G.R <mahendra at mahendragr.com> wrote:
Hello all,
I have implemented drawing polygons in a scene using QpolygonF, QGraphicsPolygonItem and QgraphicsScene. When i click on the scene, i collect the points and create a polygon on the double click, until here its fine. When i move/drag the polygon it does move. But the problem is, i have a huge data set, and when i move the polygon with this dataset in the background, polygon moves slowly, and when the cursor goes out of the polygon, i get a segmentaion fault, how should i restrict cursor to be inside the polygon item or is there a better solution for this?
My function :
void CustomScene::addPolygons( QVector<QPointF> &points )
{
QPolygonF polygon1(points), polygon2;
MyItem *item1;
QPointF p1,p2,p3,p4;
p1 = points.at(0);
p2 = points.at(1);
p3 = points.at(2);
p4 = points.at(3);
item1 = new MyItem;
item1->setPen(QPen(Qt::red));
item1->setPolygon(polygon1);
item1->setAcceptDrops(true);
item1->setFlag(QGraphicsPolygonItem::ItemIsMovable);
item1->setFlag(QGraphicsPolygonItem::ItemSendsGeometryChanges);
this->addItem(item1);
points.clear();
}
void CustomScene::mousePressEvent ( QGraphicsSceneMouseEvent * event )
{
if (addPolygonFlag == true && event->button() == Qt::LeftButton)
{
points.append(event->scenePos());
}
else if (event->button() == Qt::RightButton)
{
QGraphicsPolygonItem *tempItem;
tempItem = (QGraphicsPolygonItem*)this->itemAt(event->scenePos());
removeItem(tempItem);
}
QGraphicsScene::mousePressEvent(event);
}
void CustomScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
addPolygons(points);
addPolygonFlag = true;
QGraphicsScene::mouseDoubleClickEvent(event);
}
void CustomScene::mouseMoveEvent ( QGraphicsSceneMouseEvent * event )
{
QTextStream out(stdout);
MyItem *tempItem;
tempItem = (MyItem*)this->itemAt(event->scenePos());
if (tempItem == 0)
{
QGraphicsScene::mouseMoveEvent(event);
return;
}
tempPoly = tempItem->polygon();
//tempPoly1 = tempItem1->polygon();
QPointF p1,p2,p3,p4;
p1 = tempPoly.at(0);
p1 = tempItem->mapToParent(p1);
p2 = tempPoly.at(1);
p2 = tempItem->mapToParent(p2);
p3 = tempPoly.at(2);
p3 = tempItem->mapToParent(p3);
p4 = tempPoly.at(3);
p4 = tempItem->mapToParent(p4);
out <<"p1: "<<p1.x()<<" "<<p1.y()<<endl
<<"p2 : "<<" "<<p2.x()<<" "<<p2.y()<<endl
<<"p3 : "<<" "<<p3.x()<<" "<<p3.y()<<endl
<<"p4 : "<<" "<<p4.x()<<" "<<p4.y()<<endl;
QGraphicsScene::mouseMoveEvent(event);
}
--
http://www.mahendragr.com
--
http://www.mahendragr.com
---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.
“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”
“Please consider our environment before printing this email.”
More information about the Qt-interest-old
mailing list