Results 1 to 2 of 2

Thread: Comparing QCursor::pos() to QGraphicsItem::pos()

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Comparing QCursor::pos() to QGraphicsItem::pos()

    I have a series of tiles (QGraphicsItems), let's say 100x100 of them.
    Each are positioned 10 pixels apart using setPos().
    They all show up correctly in a QGraphicsScene.

    I'm trying to capture a QCursor position when QGraphicsView catches a mouseReleaseEvent().
    However, the pos() for the QMouseEvent and QCursor:: pos() both give me weird coordinates.
    I believe it has something to do with the level of zoom at the time of the mouse click, but I'm not sure.

    So here's some code. The tiles' positions are started at (0,0) and are 10x10 pixels. So, if a mouse clicks the top-left-most tile at (5,5), tileAt() should return that first tile in the top left corner. However, the coordinates I'm getting for the QCursor(and QMouseEvent) are usually quite a bit higher than the tiles they're clicking on.

    Here's the two relevant functions:

    Qt Code:
    1. void GraphicsView::mouseReleaseEvent(QMouseEvent *e)
    2. {
    3. Tile* tile = tileAt(mapToGlobal(QCursor:: pos())); //or tileAt(mapToGlobal(e->pos()));
    4. }
    5.  
    6. Tile* GraphicsView::tileAt(QPoint position)
    7. {
    8. for(int row=0; row<tiles.size(); row++)
    9.  
    10. if((mapToGlobal(tiles[row][0]->pos().toPoint()).y() + tiles[row][0]->height) < position.y())
    11.  
    12. for(int col=0; col<tiles[row].size(); col++)
    13.  
    14. if((mapToGlobal(tiles[row][col]->pos().toPoint()).x()+tiles[row][col]->width) < position.x())
    15. return tiles[row][col];
    16.  
    17. return new Tile(-1,-1,this);
    18. }
    To copy to clipboard, switch view to plain text mode 

    In this example, I was trying to use mapToGlobal on both the tiles' positions and the cursor position, but I still got weird numbers for the cursor. I've also tried using mapToScene on them.

    Any idea how I can compare the positions of the tiles to the position of the cursor?

    Thanks!
    Last edited by wysota; 6th March 2012 at 20:32. Reason: missing [code] tags

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Comparing QCursor::pos() to QGraphicsItem::pos()

    You've overdone it.

    Qt Code:
    1. void GraphicsView::mouseReleaseEvent(QMouseEvent *e)
    2. {
    3. Tile* tile = tileAt( view->mapToScene( e->pos()) );
    4. }
    5.  
    6. Tile* GraphicsView::tileAt( const QPointF& position) // QPointF not QPoint here
    7. {
    8. Tile* tile = NULL;
    9. QGraphicsItem* item = scene->itemAt( position );
    10. if( item && item->type() == YourDefinedTypeForThatItem ) // YourDefinedTypeForThatItem is a number returned from int type( void ) const which you have to implement in your Tile class;
    11. {
    12. tile = (Tile*)item;
    13. }
    14. return tile;
    15. }
    To copy to clipboard, switch view to plain text mode 

    Basically - you use mapToScene() once, you get QPointF ( not QPoint! ) and then you can compare that position with position of your items.
    Anyway, IMHO scene->itemAt() is the way to go for you.

Similar Threads

  1. Comparing QString and char*
    By SIFE in forum Qt Programming
    Replies: 3
    Last Post: 4th January 2012, 21:19
  2. Comparing a Qstring with a value defined in an enum
    By AndresBarbaRoja in forum Newbie
    Replies: 5
    Last Post: 24th March 2011, 16:11
  3. Best performance when comparing two QTextEdits
    By leenxkewl in forum Qt Programming
    Replies: 3
    Last Post: 16th October 2010, 19:07
  4. Comparing the |(shift+\) char
    By warry in forum Newbie
    Replies: 1
    Last Post: 8th September 2008, 07:05
  5. Comparing Items In List Box
    By kenny_isles in forum Qt Programming
    Replies: 9
    Last Post: 21st February 2007, 13:06

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.