[Qt-interest] Regarding polygons and mouse events

Malyushytsky, Alex alex at wai.com
Wed Nov 10 23:03:53 CET 2010


First, I highly recommend change your implementation of addPolygons() to allocate items using operator new.
I am not sure why it does not crash application when scene is deleted as for now, but it will lead to the problem anyway.
The reason is that scene reparents all the items added to it.
It means they operator delete will be called when clear is called. But your items are not allocated with new.

Second.  Items are movable by default. QGraphicsScene handles it. Focus item + any items selected item will be moved.
And all 3 functions: mousePressEvent, mouseMoveEvent and mouseReleaseEvent are involved.
If you want to handle it yourself you should override all three.

To demonstrate this you can just remove mouseReleaseEvent which is overridden in your class or make it call the base class member:
void CustomScene::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
{
        QGraphicsScene::mouseReleaseEvent ( event );
}

And everything will work.


Regards,
  Alex



From: qt-interest-bounces at trolltech.com [mailto:qt-interest-bounces at trolltech.com] On Behalf Of Mahendra G.R
Sent: Wednesday, November 10, 2010 1:34 AM
To: qt-interest at trolltech.com
Subject: [Qt-interest] Regarding polygons and mouse events

Hello all,

I have a (another) problem now, i managed to move polygon inside a scene using Qgraphicsscene.  Now i have multiple polygons in the scene, i want to move the polygons inside the scene.  For example if i have blue/green polygons, i want move them indiviually, my implementation does this for ONE polygon, even if i i have two polygons in the scene, whenever i click the second polygon, only the first polygon moves and the other stays idle all the time.  I have attached my code, any help will be really useful, i've almost gave up on this bug..


----------------------



void CustomScene::mouseReleaseEvent ( QGraphicsSceneMouseEvent * event )
{

        QTextStream out(stdout);
        out <<"Mouse released "<<endl;

        double mousePosX;
        double mousePosY;

        mousePosX = (double) event->scenePos().x();
        mousePosY = (double) event->scenePos().y();
        out <<mousePosX<<" "<<mousePosY<<endl;

        QPolygonF tempPoly;

        QGraphicsPolygonItem *tempItem;

        tempItem = (QGraphicsPolygonItem*)this->itemAt(event->scenePos().x(),event->scenePos().y());

        if (tempItem == 0)
        {
            out<<"Item not found  "<<endl;
            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;

}
----------------

I tried with mousePressEvent, but then i cant move the polygons at all!!, this is the best possible way i could do it (or am i missing something??)


--
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