Results 1 to 7 of 7

Thread: mouse pos() returns wrong position

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2019
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: mouse pos() returns wrong position

    Hello
    thank you for answering, and regarding the mousePressEvent(), its being called in the game class:
    This is the code :

    Qt Code:
    1. void Game::mousePressEvent(QMouseEvent *event)
    2. {
    3. if(event->buttons() == Qt::RightButton && in_room!="menu"){
    4.  
    5. QLineF ln(event->pos(),player->pos());
    6. int angle = -1 * ln.angle() + 460;
    7.  
    8. int tmp = angle + 80;
    9. if(tmp>360)
    10. tmp-=360;
    11.  
    12. if(angle > 360)
    13. angle += -360;
    14.  
    15. player->setPlayerRotation(angle,tmp);
    16.  
    17. QPointF t(event->pos());
    18. player->setDestination(t);
    19.  
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    I tried putting :
    Qt Code:
    1. QPointF t(mapToScene(event->pos()));
    To copy to clipboard, switch view to plain text mode 

    But it still doesn't work...

    And yeah they are both QGraphicsItem instances in the scene, here is the code if helps :
    Land and Water files ( both are the same just the namings are different )
    Qt Code:
    1. #ifndef LAND_H
    2. #define LAND_H
    3.  
    4. #include <QObject>
    5. #include <QGraphicsPixmapItem>
    6.  
    7. class Land: public QObject, public QGraphicsPixmapItem{
    8. Q_OBJECT
    9. public:
    10. Land(QGraphicsPixmapItem *parent=NULL);
    11.  
    12. private:
    13. QString file_name;
    14. };
    15.  
    16. #endif // LAND_H
    To copy to clipboard, switch view to plain text mode 
    .cpp
    Qt Code:
    1. #include "land.h"
    2.  
    3. Land::Land(QGraphicsPixmapItem *parent)
    4. {
    5. file_name = ":/Images/Land.png";
    6. setPixmap(QPixmap(file_name));
    7. }
    To copy to clipboard, switch view to plain text mode 

    I don't know how to make my mouse event ignore those two classes when pressing right click on them ( so that its like i'm trying to access scene only and get coordinate from it )... I tried looking for the solution on google ( ignore() functions and so on ) and i still have no idea...

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: mouse pos() returns wrong position

    mousePressEvent(), its being called in the game class
    No, it isn't being called there, it is defined there. It is probably being called from Qt's event loop, handling events on the QGraphicsView that are not being handled by other objects.

    You should read the Qt documentation on the Qt Graphics View Framework and pay particular attention to the section on "The Graphics View Coordinate System".

    You should also be sure that you are handling your mouse events correctly. There is a complete set of mouse and other events that are handled internally in the QGraphicsScene class. These take care of mapping events in the scene to the graphical objects that have the current focus or are under the mouse. If you are handling mouse events at both the widget level (QGraphicsView) and scene level, then the coordinate systems are not the same.

    My advice is to handle mouse events only at the scene level (by implementing handlers for your QGraphicsItem-based classes), and map everything to scene coordinates.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Sep 2019
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: mouse pos() returns wrong position

    No, it isn't being called there, it is defined there.
    oh i see, ye i have to take a look at view, scene and events handling stuff a little bit more, tho after trying some stuff out i realized that when i was creating a line between the player and the mouse i forgot to use mapToScene when initializing points. So far it seems to be working. Thank you very much for that !!

    Tho i have one more question, how do i put waves (image) around my island in the game as the background. I want to make it so that for example 60 x 60 pixels waves fill the background. Doing setBackgroundBrush(QBrush(QImage("path"))); will set the background the way i want it to, but the problem is when the player is moving and he gets near the water, he has to stop. I wanted to check if he is near the water using collision. Like that class Land i made class Sea and this class is also QPixmapItem and i could just check if the player has collided with Sea but the problem comes when i want the sea to be moving, cause changing pictures takes time and that's not optimal...

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: mouse pos() returns wrong position

    cause changing pictures takes time and that's not optimal...
    Changing pictures only takes time if you are loading them from disk every time you change. If you keep the pictures in memory in the correct form (QPixmap), then changing the image is as fast as calling setPixmap(). QPixmap instances use data sharing, which means that calling setPixmap() does not involve a data copy of the pixmap, simply swapping an internal reference. There is even a QPixmapCache cache that you can store your pixmaps in.

    For detecting if your player has walked into the water, you might be able to use QPolygon to define the area of the land, and QPolygon::containsPoint() to determine if your player is still on land.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. The following user says thank you to d_stranz for this useful post:

    FlyDoodle (5th October 2019)

  6. #5
    Join Date
    Sep 2019
    Posts
    20
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: mouse pos() returns wrong position

    I didn't put QPixmap on the heap, that is why it took couple of seconds to load an image every time. Thank you very much for the help !!!

Similar Threads

  1. view position of mouse position in GraphicsScene
    By Raghaw in forum Qt Programming
    Replies: 2
    Last Post: 23rd August 2012, 04:46
  2. QProcess.pid() returns wrong pid in PyQt(windows)
    By vertusd in forum Qt Programming
    Replies: 1
    Last Post: 16th April 2011, 07:57
  3. QHttp get returns the wrong page
    By SpeedxDevil in forum Newbie
    Replies: 1
    Last Post: 14th November 2010, 22:46
  4. mouseMoveEvent contains wrong position?
    By draftpunk in forum Qt Programming
    Replies: 10
    Last Post: 12th September 2008, 01:59
  5. Replies: 4
    Last Post: 27th July 2006, 11:13

Tags for this Thread

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.