[Qt-interest] mouse and polygons

Mahendra G.R mahendra at mahendragr.com
Fri Nov 12 17:01:07 CET 2010


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101112/3896b666/attachment.html 


More information about the Qt-interest-old mailing list