I'm programming my first 2D game in Qt.
I have QWidged where I draw my game (isometric view). When mouse enters border of widget it moves map view (like in every strategy game...).
And here is my trouble... I'm tracking mouse position with mouseMoveEvent but it fires only when mouse moves (only when position changes). So map moves only when I move mouse at borders. If mouse stand still, map does not move (mouseMoveEvent is not triggered). And I have no idea how to solve this. It's annoying when you try to play it.
This is my first post here.. and I hope that I explained my problem clearly :)
Edit (little more clarify):
Imagine this: you want to move map. So you move mouse to the edge of screen (QWidget) but at the moment when you stop mouse, map stops moving too. But mouse is still at edge of screen. What I want to do is that map will still move after mouse stops at edge.
You can create QPropertyAnimation for coordinates and start/stop it when mouse moves to/from widget's border.
Or you can remember current state ("changing x by -1 every 100ms, changing y by 0") and call some slot that does the real moving with QTimer.
Related
I'm trying to implement a way to click and drag the screen in SFML. The idea being that when the user clicks down on their mouse, an sf::View gets changed by how much the user moves their mouse until they release it.
I tried to use the sf::Event::MouseMoveEvent struct under the assumption it would give the mouse movement and not the position. When I used it in code, however, it gets the position.
I could try and keep track of the previous mouse position and subtract it out to get the change, but I would expect that there would be a struct or function that would give me the delta position. It has this functionality for the scroll wheel, so I'd hope it does for the mouse.
I am implementing a function (Using Qt) that is supposed to move a widget to the cursor position as part of a drag and drop functionality.
I have three events that get triggered, mouse down, mouse move, and mouse up. When the mouse moves and is down, a signal is sent to the widget to move itself to the cursor; however, I have encountered some strange behavior.
This simple code:
void Block::moveToCursor()
{
block->move(block->mapFromGlobal(QCursor::pos()));
qDebug() << block->mapFromGlobal(QCursor::pos());
}
where "block" is a QLabel that is a member of Block and is a child of the window's central widget.
It produces this result:
As it can be seen in the debug output, the coordinates are flip flopping (or flickering) every time there is a pixel move. The first coordinated are correct but the second set of coordinates seem to be relative to the top right corner of the window.
I have tried all the mapping functions:
block->move(block->mapFromParent(QCursor::pos()));
-Produces a similar result with the second set of coordinates relative to the center of the window.
block->move(block->mapFrom(this->block, QCursor::pos()));
-This produces an even stranger result. The block does not flicker and moves correctly with respect to the mouse, but the initial position of the block seems to be off by the distance from the top right corner of the computer screen. It also only shows one point when printed out in debug, yet it is moving on the screen. Every time you see Continue Drag, the mouse has moved at least one pixel.
Can someone explain this strange behavior to me and show me the correct way of moving the widget to the cursor at the exact position from where it was originally clicked?
To get the coordinate in relation to the parent (like wanted from move()-Method), you need to use the mapFromGlobal on the parent like this:
block->move(block->parentWidget()->mapFromGlobal(QCursor::pos()));
Just give it a try. The reason for this is, lets consider what happens when you are at position 1,1 inside the child. But the child is for example at position 123,123 from its parent top-left. You would now move it to 1,1, causing it to jump 122 pixels each.
https://stackoverflow.com/questions/9466359/graphics-editor-in-c
I've developed a simple graphics editor in c++.It requires me to drag the mouse to draw a shape .After drawing the shape I want to fill it by picking a color but since dragging the mouse amounts to a large number of clicks because of which the entire screen gets filled with a default color even before i've drawn the shape. delay() doesn't work either.
the mouse click event in turbo c++ has two part. one where you press the button and two when you release the button. you need to drag so you should use the clrscr() function in the loop which goes on iterating until the mouse button is pressed down along with the code for the shape you want to draw. that way your screen keeps getting updated when you are dragging the mouse. and the loop ends when you release the button.
for filling the shape using the flood fill function should suffice
Perhaps one of the following links is what you're looking for:
http://www.codeproject.com/Articles/11313/Mouse-Programming-in-C-C
http://www.cprogrammingreference.com/Tutorials/Advance_Tutorials/mouseprogramming.php
http://www.brackeen.com/vga/mouse.html
I have a GUI application where you can drag widgets around in a QGraphicsView/Scene. You can also pan and zoom while you're not dragging a widget.
What I want:
I want to make it so that when you're dragging an item near the edge of the screen that it starts to pan in that direction and keeps panning even if the mouse isn't moving, but the mouse still needs to be in the top/left/right or bottom 10% of the screen.
What I've tried:
Inside the dragMoveEvent I check if the mouse is on the edge of the screen. If so I start a Qtimer with 15msec timeout. On timeout a function is slotted to pan the scene in the right direction by updating the center. This approach WORKS, BUT when i leave the mouse still on the edge the panning is choppy and if the mouse is moving around inside the edge then the panning is smooth as it should be.
Bottom Line:
Why is the panning only choppy/laggy when the mouse isn't moving? How do I fix it so that the panning is smooth like when you move the mouse around inside the edges?
What happens when you increase/decrease your timeout?
This is my first attempt at writing a QT app, and I'm just trying to get a feel for how it works. My goal is to have a 400x400 widget which knows the exact position of the mouse when the mouse is hovering over it. For example, if the mouse was hovering in the top left corner, the position might be 10,10 (or something similar). If the mouse is in the bottom right corner, it might say 390,390.
Eventually, these coordinates will be displayed in a label on the main window, but that should be trivial. I'm stuck at the actual fetching of the coordinates. Any ideas?
For your widget, you must enable mouse tracking.
Then, you can either install an event filter, paying attention to mouse events and looking for the move event, or you can inherit from QWidget and override the mouse event, looking for mouse move events.
http://doc.qt.io/qt-4.8/qwidget.html#mouseTracking-prop
http://doc.qt.io/qt-4.8/eventsandfilters.html
http://doc.qt.io/qt-4.8/qmouseevent.html
If you are ever in a situation when you don't need actual tracking, just position at the moment, you can use QCursor::pos().