Results 1 to 7 of 7

Thread: Crazy over the QGraphicsScene/Item

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Crazy over the QGraphicsScene/Item

    I have added an customized QGraphicsPixmapItem to the Scene, but when I click on the item.. what can I say, it records it as a ordinary click on the scene, but if I click more than once very quickly, it also notice that the item is clicked.

    (as a side note: if I click exactly in the left margin of the Item, it seems to respond to a slow single click.)

    It's driving me crazy.

    Here is code for the item
    Qt Code:
    1. void Town::mousePressEvent(QGraphicsSceneMouseEvent* ev)
    2. {
    3. Qt::MouseButtons btn = ev->buttons();
    4. if (btn & Qt::LeftButton)
    5. {
    6. qDebug()<<"You visited Lamerville";
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    When I set the position for the item, I set it with integer coordinats. but I think that can hardly be the problem.

    Here is code for the scene
    Qt Code:
    1. void Game::mousePressEvent(QGraphicsSceneMouseEvent* ev)
    2. {
    3. QPointF p = ev->buttonDownScenePos( Qt::LeftButton);
    4. int x,y;
    5. x=p.x()/SKAL;
    6. y=p.y()/SKAL;
    7. qDebug()<<"you pressed mouse on map"<<x<<y;
    To copy to clipboard, switch view to plain text mode 
    SKAL is a constant for scaling purposes.

    I'd be very happy for any suggestion. I guess there is a fundamental flaw... but what is it?

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crazy over the QGraphicsScene/Item

    Did u subclass the pix item ??
    if yes, while handling mousepress event.. try passing it to QGraphicsItem::mousePressEvent ()....

    also how are town and game related ??

  3. #3
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crazy over the QGraphicsScene/Item

    Yes I did this:

    Qt Code:
    1. class Town :public QGraphicsPixmapItem {
    2. public:
    3. Town(QPoint p=QPoint(0,0));
    4. void mousePressEvent(QGraphicsSceneMouseEvent* ev);
    5. protected:
    6. QString name;
    7. };
    To copy to clipboard, switch view to plain text mode 

    In the town.cpp file I have

    Qt Code:
    1. void Town::mousePressEvent(QGraphicsSceneMouseEvent* ev)
    2. {
    3. Qt::MouseButtons btn = ev->buttons();
    4. if (btn & Qt::LeftButton)
    5. {
    6. qDebug()<<"You visited Lamerville";
    7. }
    To copy to clipboard, switch view to plain text mode 
    }

    Should I add a line
    Qt Code:
    1. QGraphicsItem::mousePressEvent ();
    To copy to clipboard, switch view to plain text mode 
    to the code? Where? In the beginning of the function?
    Exactly, what will it accomplish?

    town and game is related by populating the game ( a Scene) with towns (Items)

    Qt Code:
    1. QVector<QPoint> cities = map->randomTownPositions(8);
    2. QPoint pos;
    3. Town* t;
    4. foreach (pos, cities)
    5. {
    6. t = new Town(pos);
    7. addItem(t);
    8. towns.push_back(t);
    9. }
    To copy to clipboard, switch view to plain text mode 
    I think the "additem" will be enough, won't it?

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crazy over the QGraphicsScene/Item

    did u try adding QGraphicsItem::mousePressEvent () to ur mousepressevent ??

  5. #5
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crazy over the QGraphicsScene/Item

    Sorry, I did something to solve it. I can't remember right now.
    But I think it was somethign with mousPressEvent.. or.. no. I don't remember what it was.

  6. #6
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crazy over the QGraphicsScene/Item

    well.. if u remmeber what u did.. u may post it.. it may benefit me too if ur way is not known

  7. #7
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Crazy over the QGraphicsScene/Item

    I think it was something with adding the item to the scene or setting the parent.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.