Add mouse click event to QGraphicsView created with QtCreator - c++

Is there a way to create a event to the QGraphicsView created without creating another class and inheriting the QGraphicsView?
I want to know if there's a way to do something like that:
ui->gView->AddEvent(click, functionName);
Thanks.

If you want to handle mouse click event for the view, install an event filter to the view and watch for MouseButtonPress event.

Related

How do I pass QEvents to child widgets?

Here's the situation:
I have a custom widget subclassed from QTabWidget that I've implemented to accept QDropEvents for files. When files are dropped on the TabWidget they are opened for editing. This part works fine. However, I want to implement drag and drop functionality as part of the editor (think like a LabView-esque GUI). I have properly implemented the event handlers and acceptsDrops on the EditorWidget but the TabWidget receives all the events and attempts to process them as files. I can differentiate file-related events from the editor's events by mimedata but I can't figure out how to pass the event from the TabWidgeton to the appropriate EditorWidget.
So the question:
How can I pass a QDropEvent from the widget which received it from the system to another widget which it owns? Alternatively, how do I tell the system which widget should receive the event, based on the contents of said event?
What I've tried:
I can't call the dropEvent method of the child as it's protected. I could create a series of my own methods that pass the events around but that seems redundant and fragile. I've looked into installing an EventFilter, but from what I can tell that only discards events, it doesn't say "not me try someone else."
Thanks in advance for your assistance!
Intersting! I think that accepting the event in the parent widget, and then trying to forward it to the child widget, is not the right approach architecturally. It would basically violate encapsulation (objects handling their own events).
If I were you, I would investigate why the child widget isn't seeing the event first. Children widgets are on top of their parents, so your child widget should have a first go at the event. Did you call setAcceptDrops(true)?
When you fix that, in the child widget event handler you can analyze the event and call event->ignore() if the event should be forwarded to the parent QTabWidget. If you don't call ignore(), the child will "consume" the event and it will not be propagated to the parent!
Here's an old blog post on event propagation that could help:
http://blog.qt.io/blog/2006/05/27/mouse-event-propagation/
Solving my own problem:
As Pliny stated the child should see the event first. My problem appears to have been that in EditorWidget I had not implemented dragEnterEvent and dragMoveEvent so even though I had implemented dropEvent in EditorWidget the TabWidget took control of the drag and therefore stole the drop.

intercept and ignore a wheel event in a QTabWidget

I implement a UI with Qt. My UI is a mainwindow containing some widgets, and a supervisor to manage my widget behaviors. In one of my widget, outputDataWidget, I have a QTabWidget, and some other widgets.
In my tabWidget, I have a dataEditor to show points, and I create a "+" tab, to create a new tab. The tab creation is done by a connect function, which is in the mainwindow:
connect(outputWidget.get(), SIGNAL(SIG_createNewOutput(int)), projectSupervisor.get(), SLOT(sCreateNewOutput(int)));
My problem is: when we use the wheel mouse button in the tab header to go rigth, some new page are created. I would like intercept the signal and avoid the creation of a new tab.
I try to add a filter event in the mainwindow and in the outputdatawidget to ignore the wheel event. Then I try to reimplemeted wheelEvent(QWheelEvent* we) and event(QEvent *e) of the QTabWidget. Each time, we enter in thiese modification after call the connect (create the tab).
Have you an idea how to call the event before the connect, or how I can disable the wheelevent of a QTabWidget?
Thanks
You'll have to subclass QTabBar to handle the QWheelEvent, then call QTabWidget::setTabBar during initialization.
This is necessary because QTabWidget internally uses a QTabBar with Qt::WheelFocus, so the wheel event never propagates to the tab widget.

How to handle right button clicks in QTreeWidgetItem?

I'm implementing something similar to Eclipse's Package Explorer, using QTreeWidget, but I don't know how to handle right mouse button clicks.
How do I use Qt creator so I can handle right clicks on a QTreeWidgetItem?
You can set the context menu policy on the tree view item and then create a signal/slot event handlers as per usual.
For an example you can refer to this:
ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->treeView, SIGNAL(customContextMenuRequested(const QPoint &)),
this, SLOT(onCustomContextMenu(const QPoint &)));
Then just implement the onContextMenu function above

How to override menuitem click event in QtDesigner?

I'm trying to make a C++ Qt Widget Application using QtDesigner,
I have to override some menu items clicked events,
so how to override the QAction clicked event from .ui?
If I understand well you are looking for this:

how to add child window controls to CWindowImpl

How do I add simple child window controls (e.g. a button) to a CWindowImpl?
I've looked at CWindowImpl and CDialogImpl. With CDialogImpl, it appears that you just create a dialog template resource and use it, very simple. I would like to do something similar with CWindowImpl, but there doesn't seem to be a way to do it. Must I add the controls manually and position them programmatically?
Some context on what I'm trying to do: I'm trying to create a plug-in for foobar2000, a Windows audio player. I would like to create a "UI element" plugin, and in the sample code that I've looked at, a "UI element" is created via CWindowImpl. How do I add buttons to this CWindowImpl? I've tried using a CDialogImpl instead, but this gives me a "pop-up" dialog, which is not what I'm looking for.
Thanks very much in advance!
With any window, including CDialogImpl, you can add child controls by creating a new window of control class and specifying your parent window handle as a parent for new control. Additionally, SetParent API is here to re-parent any window.