Qt - how to glue two windows and move them together? - c++

Like the qmmp(Qt) music player ui design, these two or three windows are in fact in the same window, because there is only a dock icon, and these windows can move together and attach to each other.
I read the source code, it seems use QDockWidget. But I really don't know the details how to get it.

When you manually move the secondary window, in this case - the playlist, you check where the manual move ends, and if it is on the edges of the primary window, you glue it by simply binding its position to the position and dimensions of the primary window.
Since the window position and dimensions are properties, they have notification signals, so you can connect those to a function that automatically moves the glued window.
And finally, when you attempt to manually move the secondary window, you un-glue by disconnecting.
You can easily support offset gluing instead of a purely horizontal or vertical one, by calculating and storing the positioning offset and applying it on every primary window move.
If the drop happens within a given threshold of the primary window you can snap to the edge. If you factor in the mouse position relative to the dragged window, you can even snap particular edges together.

Related

How to select a specific sprite and move it in SDL

I'm making a chess game, where I have a box shaped selector, now I want to select a sprite with that box and move it in a specific location with the selection box.
Any link or code?
You keep track of the position of everything.
Poll for events, see where the mouse is. If in the rectangle of a piece, put the selector on top of the piece (assuming you have colorkeying or alpha). You should be able to listen to keyboard input and move the selector based on that.
When mouse clicks, see where the selector is. Find the piece that's under it, and remember it.
When mouse clicks again, see where the selector is now. Check if moving there is valid. If yes, move the piece you remember to the new position.

How to move mouse to second screen monitor?

I'm making an application that needs to programmatically move the mouse pointer.
My ide is VC++ 2013.
I'm using the winuser.h header with this function:
SetCursorPos(int x, int y);
But this moves the mouse only on the primary screen.
For the case that has only one monitor this works fine.
But if I'm in presentation mode, with 2 monitors I need to move the mouse to the second monitor.
so, I to need detect if has 2 monitors and if it has, move the mouse to the second monitor.
Conceptually, the mouse moves (and windows are positioned) on a virtual desktop - a large desktop surface that spans all monitors. A monitor is a viewport onto this surface - it shows whatever happens to fall into a certain rectangle.
EnumDisplayMonitors gives you the list of all monitors, and for each a rectangle that this monitor reveals on the virtual desktop. To move a mouse cursor to a particular monitor, simply move it to a point that falls within the rectangle corresponding to that monitor.

Qt MouseMoveEvent only triggers with a mouse button press

I have an odd problem here.
I'm working on an application, and within one of my classes I'm monitoring my mouse events.
The weird thing is, my mouse move event will only get called if any mouse button is pressed.
I'm not even filtering for any button presses within the method; the method itself doesn't even get called unless I click on this object itself (the one that's monitoring it).
What generally causes this type of error to happen?
I'm not sure if it's relevant, but I have 2 different things monitoring my mouse inputs: 1) the main program monitoring the global mouse coordinates, and 2) an object within my program monitoring the mouse coordinates within itself.
Edit
So the problem has to be because mouse move event is generally used when people are dragging the cursor along the screen right?
My reason for not needing it like that is because I'm building a custom context menu of sorts, and I need to know when an item is hovered over.
It turns out that I didn't truly set everything within my class to enable mouse tracking.
I somehow thought if the class itself was set to have it enabled, I wouldn't need to set it to all the sub objects, but now I see how that wouldn't make any sense at all.
So just to clarify my solution:
The items that I needed to track my cursor's position needed to have
setMouseTracking(true);

Mouse click on a background window without moving mouse c++

I need to know if there is a code in c++ that allows to simulate a click, on a background window (if that's impossible, a foreground window will do fine), without moving the mouse.
I also need to click on specific coordinates, and drag an item to some other position (always without moving the mouse).
Example:
my mouse pointer is at (500,700),
but i need to left click at (100,150),
and drag to (700,300).
I need to be able to move my mouse pointer without affecting the program,
and the program must run correctly without moving, or locking, the mouse pointer.
If this action is impossible in c++, a VB code will be apreciated.
From a C++ Windows application, you can call Windows API directly by first finding the window using FindWindow that will give you the window handle you want, then finding the area within this window that you want to click. You can use API like GetWindowRect for this.
Finally, you can send this window, or an area within it, mouse messages using mouse_event function to cause the mouse to move, click, drag and let go at a new location.

Allow user to draw a drag rectangle in CStatic C++ MFC App

I have a MFC application where I have a Picture Control in the dialog. Eventually, I want to allow a user to draw a resizeable rectangle via mouse drag in the picture control over an image that I loaded.
I defined my own picture control class as a sub class of CStatic and am working with the mouse down, mouse up, and mouse move events but I can't seem to figure out how to allow the user to draw a rectangle. Any guidance on this would be appreciated.
Most of the examples I've looked at show me how to draw a rectangle in a CView:CWnd, but I'm not too familiar with MFC yet so I'm a bit lost. Thanks.
The usual technique for drawing a drag rect on top of the window contents is illustrated here:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd145184(v=vs.85).aspx
That is Win32 API coding instead of MFC coding but the differences are minimal. The basic idea is that by drawing with SetROP2(hdc, R2_NOTXORPEN); you invert the existing pixels, then drawing the same rect again re-inverts those pixels back to the original image.
When the user clicks the mouse button you need to record the mouse coordinates so you know where the rectangle starts. You should also set some type of flag to indicate that the user is dragging the mouse. When the user moves the mouse get the current mouse position and use DrawDragRect or similar function to draw the rectangle. When the user releases the mouse button clear the previously mentioned "flag" and you're done with that part of the process.
You will also need to handle other events such as the control and/or parent window losing focus so that you can cancel the drag/draw operation. Since you did not include any code in your question it's hard to say what else you will need to do but those are the basics.