Results 1 to 10 of 10

Thread: Keep Cursor in a widget

  1. #1
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Keep Cursor in a widget

    Hello dear QT-Community,

    i am developing kind of a game and encounter the following problem: I have a GraphicsScene which represents the map of the game. With a GraphicsView I display this map. Now I want to remove the scrollbars (haven't done this yet but can't be that hard) and use the mouse to scroll now. At the right side of the Map there is a Panel which displays details about the current GraphicsItem. I think everyone who ever played a strategy game on PC can imagine what I mean
    So scrolling shall occur when the mouse reaches one of the edges of my GraphicsView widget. And of course the Cursor must not be allowed to leave that widget. Unfortunately i was not able to implement this in a elegant way.

    When I try something like this, my Cursor is always removed from the View widget when I try to enter it. I set it in the center of the View before. But it gets thrown out immediately.

    Qt Code:
    1. void MapView::mouseMoveEvent(QMouseEvent *event){
    2. if (event->type() & QEvent::Leave){
    3. QPoint newPos(event->x(), event->y());
    4. this->mapToGlobal(newPos);
    5. this->cursor().setPos(newPos);
    6. QPointF newCenter = currentCenter();
    7. centerOn(newCenter.x() + this->width()/10, newCenter.y() + this->height()/10);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Any ideas are very welcome.

    Regards,
    Simon

  2. #2
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Keep Cursor in a widget

    I'm not sure if this will help you but check this function
    grabMouse ();
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  3. #3
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Keep Cursor in a widget

    i always get a runtime eror when i include this line in my code like that:

    Qt Code:
    1. GAMainWindow::GAMainWindow(){
    2. QHBoxLayout* layout = new QHBoxLayout();
    3.  
    4. scene = new MapScene();
    5.  
    6. view = new MapView(scene, this);
    7. view->centerOn(1000, 1000);
    8. connect(view, SIGNAL(changeFullScreen()), this, SLOT(changeFullScreen()));
    9. connect(this, SIGNAL(mouseMovedOutOfView(QPoint)), view, SLOT(mouseMovedOutOfWindow(QPoint)));
    10. layout->addWidget(view);
    11. view->setMouseTracking(true);
    12. view->grabMouse();
    13.  
    14. panel = new MapPanel();
    15. layout->addWidget(panel);
    16.  
    17. connect(view, SIGNAL(mouseMoved(QPointF,QPointF)), panel, SLOT(newCoordinates(QPointF,QPointF)));
    18.  
    19. QWidget* widget = new QWidget();
    20. widget->setLayout(layout);
    21.  
    22. setCentralWidget(widget);
    23.  
    24. connect(scene, SIGNAL(sendData(int)), panel, SLOT(showID(int)));
    25. }
    To copy to clipboard, switch view to plain text mode 

    If I include the mouseGrap like this i get no Runtime error:

    Qt Code:
    1. void MapView::keyPressEvent(QKeyEvent* event){
    2. switch (event->key()){
    3. case Qt::Key_B:
    4. this->grabMouse();
    5. break;
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    But now the MapView won't get any MouseMoveEvent. The MainWindow still gets them but here the track does not work. I i set MainWindow->setTrackingMouse(true) nothing is different.

    It would work if my MainWindow AND my view would be able to track the mouse. Unfortunately they seem to be unable

    Do you have any idea why I always get the runtime error when include grapMouse staticly?

  4. #4
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Keep Cursor in a widget

    Check this link
    http://qt.nokia.com/doc/3.3/geometry.html
    and the attach example. This will work partially.
    The mouse move event will detect global coordinates and put the mouse inseide the cliente area, if he gets near the margins.
    The problems his that if the users moves to quiclly the mouse will move outside anyway. And once outside the client area it will not call
    the mouse event function.
    A workaround could be a timer to call mousemove event, then the mouse will always be put inside the client area.

    I remenber doing this in Visualc MFC, there was function SetCapture() that would capture the mouse inside the cliente area, but I didnt find any Qt similar function.
    Attached Files Attached Files
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  5. #5
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Keep Cursor in a widget

    Thanks for your answers so far. This is basicly what i tried before. Your example works fine for me and I agree with you that one could solve the issue of fast mouse moving with a timer. But if I try to implement this in my program it does not work. I had a look at the debugger and i think my geometry does not work properly. And I think this might be because i did something wrong with the derivation of my classes.

    Would it help if I post my whole code here or what informations are required to help me with this issue?

    Edit:
    I have the problem that the upper and the left margin are ca. as twice as big as the lower and the right margin. Anyone has an idea why this is the case?

    Qt Code:
    1. void MapView::mouseMoveEvent(QMouseEvent *event){
    2. int marginThickness = 30;
    3.  
    4. int globalX = event->globalX();
    5. int globalY = event->globalY();
    6.  
    7. int geoX = geometry().x();
    8. int geoY = geometry().y();
    9.  
    10. QPoint globalGeo (this->mapToGlobal(QPoint(geoX, geoY)));
    11.  
    12. QPoint newPos(globalX, globalY);
    13.  
    14. if (globalY < globalGeo.y() + marginThickness){
    15. newPos.setY(globalGeo.y() + marginThickness);
    16. } else if (globalY > globalGeo.y() + height() - marginThickness){
    17. newPos.setY(globalGeo.y() + height() - marginThickness);
    18. }
    19. if (globalX < globalGeo.x() + marginThickness){
    20. newPos.setX(globalGeo.x() + marginThickness);
    21. } else if (globalX > globalGeo.x() + width() - marginThickness){
    22. newPos.setX(globalGeo.x() + width() - marginThickness);
    23. }
    24. this->cursor().setPos(newPos);
    25. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by TheClerk; 25th January 2010 at 10:35.

  6. #6
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Keep Cursor in a widget

    Remove mapToGlobal from line 10, you dont need it sense geometry() is already global. Then check if it works
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  7. The following user says thank you to john_god for this useful post:

    TheClerk (29th January 2010)

  8. #7
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Keep Cursor in a widget

    I am afraid this does not work. If I remove the mapToGlobal the following happens:

    The area that can't be left by the Cursor stays in the top left corner of the screen. If i move the application a bit down and a bit right the boarder to the bottom and to the right become larger and i can leave the application to the top and the left of the frame because it won't get any mouseEvent then.

    Edit:
    If you want i can send you the whole code zipped. I think this makes things much easier.

  9. #8
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Keep Cursor in a widget

    Can you show me a printscreen of your game, and point the area where the cursor should be grabbed ?
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  10. #9
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Keep Cursor in a widget

    Okay. Here we go:



    This is how it looks at the moment. All things painted in red are included by me with photoshop and not part of the program. I want to keep the Cursor within the red rectangular. A and B indicate the width and the height of QGraphicsView. C and D show the actual Position of the Cursor in the QGraphicsView. This works correct so far. I just can't get the correct coordinates to set the Cursor when it is close the edge of the widget.

    Thanks for your constant help by the way. I am sure we can manage this

    Regards,
    Simon

  11. #10
    Join Date
    Jan 2010
    Posts
    8
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Keep Cursor in a widget

    I figured it our myself now. Thanks for your help though.

    Regards

Similar Threads

  1. Replies: 2
    Last Post: 2nd October 2009, 15:32
  2. Validator: force keeping cursor back in widget
    By antarctic in forum Qt Programming
    Replies: 1
    Last Post: 8th June 2009, 14:15
  3. Replies: 4
    Last Post: 3rd March 2008, 22:15
  4. How to synchronize text cursor and cursor in QTextEdit
    By kennyxing in forum Qt Programming
    Replies: 6
    Last Post: 18th February 2007, 09:14
  5. cursor
    By mickey in forum Newbie
    Replies: 1
    Last Post: 14th July 2006, 19:41

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
  •  
Qt is a trademark of The Qt Company.