Help with qt4 qgraphicsview - events

I've done lots of stuff with pygtk however i'm deciding to learn pyqt, im stuck at the qgraphicsview i have absolutley no idea how to get signals from the items i place on the graphics view, primarily mouse events.How do i get the mouse events from idividual items in a scene?

QGraphicsItem is not a QObject and cannot send signals, nor receive slots. Instead, you must handle events. You can do that either through an event filter, sub-classing the view or scene to intercept events or simply sub-classing the items themselves and implementing the event handling functions (see protected member functions in the documentation). Perhaps this example can be of interest: http://doc.trolltech.com/4.6/graphicsview-diagramscene.html .

Right after you create an item, connect the signals you want from it to the instance of the widget that contains it.

Another option is to just give up using signals and have your instance of QGraphicItem directly call a method of its parent by keeping a reference to it. This is less pretty than using signals but ultimately, it gets the job done.

Related

Window control receives mouse-wheel events but not scroll-bar events

I have a dialog-box (derived from CDialog).
Inside it, I have a window control (CWnd) in which I display a bitmap image.
I would like this window control to be scrollable, so I create it as follows:
m_Window = CreateEx(0,
WC_STATIC,
NULL,
WS_CHILD|WS_VSCROLL|WS_HSCROLL|SS_BITMAP,
{0,0,width,height},
this,
0);
Now, it receives mouse-wheel events, but it doesn't receive scroll-bar events (in other words, it reaches the OnMouseWheel handler, but it doesn't reach the OnVScroll and OnHScroll handlers).
What exactly am I missing here?
I tried adding an SS_NOTIFY flag to the window style when creating it, but no luck with that.
I also tried calling EnableScrollBar(SB_BOTH,ESB_ENABLE_BOTH), but no luck with that either.
One thought I have in mind, is that the WC_STATIC class type is simply not designated for that.
I tried looking for a more suitable class type by jumping to the definition of WC_STATIC in file CommCtrl.h and searching for other class types (putting #define WC_ in the search-box).
But there are too many of them, and I'm not even sure it's the right direction.
Anyone familiar with this problem?
Thank you.

JavaFX drag in order to modify nodes

I'm here because my research and testing lead me nowhere.
I have made a little calendar application and now I want to improve the user interface with nice controls. I'm trying to achieve an interface where you can create an event by dragging over it (as you can do in Google agenda, in fact this a school project and I want to reproduce this week view).
For this purpose I have tried this approach:
For each cells of the area (a GridPane that contain VBoxes) I attach handlers that achieve that feature (setOnDragDetected, setOnMouseDragEntered and setOnMouseDragReleased). And it does the job well but in some case this don't work (if the mouse move over another event, which do no have the handlers, the drag feature stop).
As I saw this too heavy and subject to bogus (3 handlers x 7 columns x 48 rows = 1008 handlers !! ), I thought about other approach but I'm a beginner in javaFX and I don't know if there is other mechanism to perform this. (I thought about a transparent layout/pane that will handle this feature and act like a "touch screen" over my application but I did not found satisfying answers). Can someone enlighten me about this kind of problem ? For a good example of what I try to achieve take the behaviour of the Google agenda for an event creation with the mouse (I do not need to manage overlap of event). Thanks in advance !!
I post a picture of what the actual view look like.
So as Tomas Mikula suggest me I only attached my handlers to the GridPane (and not to each "cell" as I made previously): setOnDragDetected, setOnMouseDragOver, setOnMouseDragReleased and setOnMouseDragExited.
As the handlers are triggered on the GridPane, moving the mouse over other element doesn't stop the feature.
With this I reduced the number of handler, simplified the code (As I do not need to manage extra behaviours as mover over another child).
Thanks !

Unity global mouse events

Most Unity tutorials suggest using Mouse events within the Update function, like this:
function Update () {
if (UnityEngine.Input.GetMouseButton(1)) {
}
}
This strikes me as really inefficient though, similar to using onEnterFrame in AS or setInterval in JS to power the whole application - I'd really prefer to use an events based system.
the OnMouseDown() method is useful, but is only fired when the MouseDown is on the object, not anywhere in the scene.
So here's the question: Is there a MouseEvent in Unity for detecting if the mouse button is down globally, or is the Update solution the recommended option?
This strikes me as really inefficient though, similar to using
onEnterFrame in AS or setInterval in JS to power the whole application
- I'd really prefer to use an events based system.
As already pointed out in comments, this isn't necessary less efficient. Every event based system is probably using a polling routine like that behind the scenes, updated at a given frequency.
In many game engines/frameworks you are going to find a polling based approach for input handling. I think this is related to the fact that input update frequency is directly correlated to the frame rate/update loop frequency. In fact it doesn't make much sense to listen for input at higher or lower frequency than your game loop.
So here's the question: Is there a MouseEvent in Unity for detecting
if the mouse button is down globally, or is the Update solution the
recommended option?
No there isn't. Btw if you want you can wrap mouse input detection inside a single class, and expose events from there where other classes can register to.
Something like:
public class MouseInputHandler : MonoBehavior
{
public event Action<Vector2> MousePressed;
public event Action<Vector2> MouseMoved;
...
void Update()
{
if (Input.GetMouseButton(0))
{
MousePressed(Input.mousePosition);
...
}
}
}
Like stated, you can use it without major concerns, Unity will 'make its magic' internally as to set processing power sensitive code execution for you in terms of polling events. That's the beauty of a modern game engine after all. You normally shouldn't have to be hacking your way around a common feature such a mouse click detection.
However if you don't want to go using the main Update() you can make a CoRoutine if you feel more comfortable with that, just bear in mind that Unity coroutines are not multi-threaded neither, so at the end everything needs to wait anyway.

X11: Removing event from queue

I'm creating an FPS demo using an Engine called: Gameplay. I'm currently trying to define a captureMouse() function into the engine so the player can look around the map. I've already been able to pin the cursor to the center of the window and turn it invisible, but as I move the mouse the screen (camera) seems to "vibrate" as it moves around. After a lot of tinkering with X11 functions I figured that the XWarpPointer() function I'm using to warp the cursor back to the center of the window is adding a "mouse moved" event to the event queue.
X11 Question: How can I identify and remove an event from the event queue before it is captured by the event cycle?
Question: Has anyone been a similar problem and solved in a different manner? If so, what did you do?
I'm sorry if I'm not being clear. I have no extensive knowledge of X11, but I really need to add this to the engine so I can, in turn, add it to my game.
I guess you're using XtAppMainLoop to handle your events.
This is actually a call to XtAppNextEvent followed by XtDispatchEvent.
If you replace the XtAppMainLoop with a loop calling XtAppNextEvent to get the next event and check its type (the type field of the XEvent structure).
If you want to handle the event call XtDispatchEvent, do nothing to ignore it.
The loop needs to exit when XtAppGetExitFlag returns true (or add your own exit flag).

Handling mouse events dependent on modifiers

I've written a few relatively trivial GUIs in WxWidgets and Qt, and I'm persistently unsure how to architecturally handle the following situations:
You catch a mouse event to do something to a graphical object in your GUI
What you do with the object depends on which modifier keys the user is holding down
At the start I usually do something like the following:
void MyClass::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
{
if (event->modifiers() & Qt::AltModifier) {
// do something
} else if (event->modifiers() & Qt::ControlModifier) {
// do something else
} else {
// do yet another thing
}
}
// Repreat ad-nausium for other mouse click/move events...
Eventually, with similar if/else/switch code in lots of mousePressEvent, mouseReleaseEvent handlers, this seems a bit unwieldy so I try and encapsulate some of the repetition by putting the object into different "modes" depending on which modifiers are down. Nonetheless, I still feel I'm missing some nice elegant solution.
I've tried to look at the code of various open-source tools but I've not found anything tangible (erm, simple) enough to point me in a different direction.
Some tools (say, the GIMP) seem to have so many rich and varied tool- and modifier-dependent behaviours that I feel there's got to be a nice way of architecting this pattern.
Event handling in such a GUI toolkits decides what to do according to an event and an event handler you provide. What you need is a way to decide what to do according to an event, modifier and an event handler. So you can based on your events and modifier call special event processing object in all of your standard event handlers for events provided by the toolkit. What you have to implement is that event processing object, which will according to an even and modifier call the right behaviour (event+modifier handler). This is what I would call Chain of responsibility design pattern.

Resources