How to disable scrolling functionality on wheel event QGraphicsView Qt C++ - c++

I have a graphics view and i have set my own function for scrolling by hand drag when the user presses control and mouse clicks.
I have removed the scroll bars but the mouse wheel will still scroll and even scroll past the image that's being displayed in the qGraphicsView showing blank (white) space which my hand drag doesn't.
How do i make it so wheel just straight up does nothing at all?
I know there is a function I can just put in my code without a derived class because I asked this once before and got the right answer but the answer was removed and i hadn't saved the code :(
The below code does nothing even close to the expected functionality, on start up I get the mouse still doing something message and then all clicks and wheel events everything just displays that second message ... so yea not working
bool GUI::eventFilter(QObject *object, QEvent *event)
{
if (object == ui->graphicsView && event->type() == QEvent::GraphicsSceneWheel)
{
std::cout << "Wheel Blocked";
return true;
}
std::cout << "Mouse Wheel still doing something";
return false;
}
and then this code to install the filter
ui->graphicsView->installEventFilter(this);

Install an eventfilter and filter for
QEvent::GraphicsSceneWheel
Create something like this in your app
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (object == ui->graphicsView->viewport() && event->type() == QEvent::Wheel) {
qDebug() << "SCroll";
return true;
}
return false;
}
and install to your widget.
yourwidget->viewport()->installEventFilter(this);
Edit:
I thought it should work with QEvent::GraphicsSceneWheel. If anyone knows why works with Wheel, please, explain

Also, you can to modify QGraphicsView using inheritance
class MyCustomGraphicsView : public QGraphicsView
{
virtual void wheelEvent ( QWheelEvent * event )
{
//here you can modify wheel-event
}
}
It is easy way, because qt-designer can associate GraphicsView element on your form with your inherited class

Related

How to disable scrolling in QGraphicsView?

I am trying to implement mouse wheel zoom in/out. It works BUT when I zoom in/out, the image is getting smaller AND scrolls up/down at the same time with zoom function. Look like events exist at the same time and work together.
I cannot find how to disable scrolling with mouse wheel. May be there is a way to make possible scrolling only with mouse cursor (by clicking on scrollbar).
I was overriding the main method of mouse wheel but it was causing the effect I wrote above
void MainWindow::wheelEvent( QWheelEvent* event )
Solved by using event filter. The code below provides zoom in/out with holding ctrl button.
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::GraphicsSceneWheel)
{
ui->GV_image->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
double scaleFactor = 1.15;
bool ok = QApplication::keyboardModifiers() & Qt::ControlModifier;
if (ok)
{
QGraphicsSceneWheelEvent *scrollevent = static_cast<QGraphicsSceneWheelEvent *>(event);
if (scrollevent->delta() > 0)
{
ui->GV_image->scale(scaleFactor, scaleFactor);
}
else
{
ui->GV_image->scale(1/scaleFactor, 1/scaleFactor);
}
}
event->accept();
return true;
}
return false;
}
Put this line into your constructor or other your init function
QGraphicsView *GV_image;
...
ui->GV_image->scene()->installEventFilter(this);

Qt. Ignore any mouse events in a widget over another one

I have a QTableView where a small mark appears if the mouse hover on active cell. This mark is a widget and emit a signal if hover, changing the selecion mode of the table when I drag over the table.
The problem is that if I am over the mark, I can't drag over the table.
Things I have tried:
Set the widget mark like setWindowFlags(Qt::WindowTransparentForInput);, but I can't use it because I need hover event.
Ignore the events in the widget mark using event->ignore() or sending the event to parent using and eventFilter:
bool EventFilterMarca::eventFilter(QObject *obj, QEvent *event)
{
if( event->type() == QEvent::HoverMove)
{
....
}
else if (event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease ||
event->type() == QEvent::MouseMove ||
event->type() == QEvent::MouseButtonDblClick)
{
//QApplication::sendEvent(parent(),event);//one try
//event->ignore();//another try
return QObject::eventFilter(obj,event);;
}
}
Subclassing mousePressEvent, mouseReleaseEvent and mouseMoveEvent in the mark widget and call to parent class. Looks like if it works (pass the event to parent) into the current cell of the table (the parent):
void Marca::mousePressEvent(QMouseEvent *event)
{
//event->setAccepted(false);
if(event->buttons() == Qt::LeftButton)
{
MiTabla* tabla = qobject_cast<MiTabla*>(parent());
if (tabla)
{
tabla->mousePressEvent(event);
//QApplication::sendEvent(parent(),event);
}
}
//event->ignore();
}
Well, the question is how could get that the behaviour of the table was the same if I am over the widget of the cell or directly on the cell.
Also I add a link with my first approach to getting it. It works but the code is awful and not easy to follow:
https://github.com/exodehm/tablacalc
I think the event filter is not what you want. This is what makes a widget not receive specific events. What you actually want to do is to mark the event to be unhandled by specific widget, putting it higher in the class for handling. In order to do that you should try and reimplement either the widgets ::event method, or more specific handler.
In qt docs it says that:
bool QWidget::event(QEvent *event)
This function returns true if the event was recognized, otherwise it returns false. If the recognized event was accepted (see QEvent::accepted), any further processing such as event propagation to the parent widget stops.
So what I think you should be doing is basically marking event as not accepted when specific event type you want to propagate to parent widget happens for your widget.
So in your case I would expect something like:
void QWidget::dropEvent(QDropEvent *event) {
event->setAccepted(false);
}
Also in order for this to work your table needs to be parent widget of the mark widget.

Make QToolButton show menu on top in Qt 5.9

Is there a way for a QToolButton to display its associated QMenu on top of it instead of below?
I have seen this answer which pleased me but it's in Python and I don't really know how to port it properly.
I also took a look at the source code for QMenu but it's quite overwhelming so I don't really know how to tackle this issue.
There also is a little arrow on the button showing that it'll pop down.
This is the very bottom of my window so I'd like it to pop up in case it becomes too big to fit.
You could do it using an event filter:
QMenu* yourMenu;
yourButton->setMenu(yourMenu);
yourMenu->installEventFilter(this);
bool yourClass::eventFilter(QObject * obj, QEvent *event)
{
if (event->type() == QEvent::Show && obj == yourButton->menu())
{
QPoint pos = /*the position expected*/;
yourButton->menu()->move(pos);
return true;
}
return false;
}
To remove the little arrow, add this to your stylesheet:
QToolButton::menu-indicator{
image: none;
}

Qt - mouse events on a QLabel

I've got this test piece of code in mainwindow.cpp:
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseMove)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
qDebug() << QString("Mouse move (%1,%2)").arg(mouseEvent->pos().x()).arg(mouseEvent->pos().y());
}
return false;
}
I just want to get mouse position when clicked, and send the coordinates to another thread with an OpenCV loop that'll pull HSV information and do things accordingly. I'm using mouse over just for testing.
The problem is that I have no idea how to attach this (tracking, clicking) to a QLabel labelKalibracja, one I use to display video frames, instead of the whole window.
ui->labelKalibracja->installEventFilter(this);
is supposed to work, but doesn't, but
qApp->->installEventFilter(this);
Will make the whole window a mouse track zone.
You should check the object of the event filter :
if (qobject_cast<QLabel*>(obj)==ui->labelKalibracja && event->type() == QEvent::MouseMove)
{
...
}
Now you can make sure that the event is for the label. Note that the event filter could be installed on multiple objects and it's your duty to identify the combination of objects and events.

QIcon inside combobox

I want to include a "remove" icon on entries in my QComboBox, but I am having trouble catching the mouse press event. I've tried to catch it on the combobox, and I've tried reimplemting the QIcon class to catch the mousepress there. No dice. Does anybody know how to do this?
-D
I've written code a bit like this, where I wanted to put a tree view inside a combo box and I needed to take an action when the check box on the tree was clicked. What I ended up doing was installing an event filter on the combo box to intercept mouse clicks, figure out where the mouse click was happening, and then take an action. Probably you can do the same kind of thing with your icon. Here is the code:
bool TreeComboBox::eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::MouseButtonRelease)
{
QMouseEvent* m = static_cast<QMouseEvent*>(event);
QModelIndex index = view()->indexAt(m->pos());
QRect vrect = view()->visualRect(index);
if(event->type() == QEvent::MouseButtonPress &&
(model()->flags(index) & Qt::ItemIsUserCheckable) &&
vrect.contains(m->pos()))
{
// Your action here
ToggleItem(index);
UpdateSelectionString();
}
if (view()->rect().contains(m->pos()))
skipNextHide = true;
}
return QComboBox::eventFilter(object, event);
}
Maybe you can reimplement QComboBox::mousePressEvent(QMouseEvent *e) and use e.x() together with QComboBox::iconSize() to find if the event occurred over the icon.
This will off cause break if a Qt style decides to switch label and icon position in combo boxes. Don't know if that is possible?