If this is your first visit, be sure to
check out the
FAQ
by clicking the
link above. You may have to
register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.
Welcome to
Qt Centre
.
Qt Centre
is a community site devoted to programming in C++ using the
Qt framework
. Over 90 percent of questions asked here gets answered. If you are looking for information about Qt related issue —
register
and post your question.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our
free
community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please,
join our community today
!
If you have any problems with the registration process or your account login, please
contact us
.
Hi there,
I want to scroll a Scene in QGraphicsView with external contol. So i want to disable scroll event with keys an wheel directly in the QGraphicsView class.
How can I do this?
Hi, you could install an event filter and catch the key and wheel events there. The documentation for QObject::installEventFilter() shows an example how to "eat" an event.
Ginsengelf
{
public:
MyClass
(QObject *parent
= nullptr
);
};
MyClass
::MyClass(QObject *parent
) :{
this->installEventFilter(this);
}
{
if (event
->type
() == QEvent::GraphicsSceneWheel) {
// QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event);
return true;
}
else
{
// standard event processing
return QObject::eventFilter(obj, event
);
}
}
class MyClass : public QGraphicsScene
public:
MyClass(QObject *parent = nullptr);
bool eventFilter(QObject *obj, QEvent *event) override;
MyClass::MyClass(QObject *parent) :
QGraphicsScene(parent)
this->installEventFilter(this);
bool MyClass::eventFilter(QObject *obj, QEvent *event)
if (event->type() == QEvent::GraphicsSceneWheel)
// QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event);
return true;
// standard event processing
return QObject::eventFilter(obj, event);
To copy to clipboard, switch view to plain text mode
I have installed an event filter in my inherited class "Myclass". But it has no effect.
The wheel event is still active, if I scroll the mouse wheel in the scene.
Your class inherits from QGraphicsScene, which is simply a container for QGraphicsItem instances. It has no visual representation on screen and therefore has no mouse, wheel, or any other input event associated with it.
You probably want to install your event filter on QGraphicsView (and not on a class derived from it, as you have done with your MyClass implementation). It makes no sense to create and install an event filter on an instance of a class itself - if you are going to derive from a class, then simply override the base class methods for the events you want to handle.
The purpose of an event filter is so you can watch events occurring in an instance of one class from a different class. So in your case, you should create the event filter method in your MainWindow class (for example) and install it on your QGraphicsView class.
class MouseEventFilter
: public QObject{
Q_OBJECT
public:
MouseEventFilter
(QObject *parent
= nullptr
);
protected:
};
MouseEventFilter
::MouseEventFilter(QObject *parent
) :{
}
{
// The event is always eaten but the scroll is still active
if (event
->type
() == QEvent::Wheel) {
QWheelEvent *wheelEvent
= static_cast<QWheelEvent
*>
(event
);
qDebug("Ate key press %d", wheelEvent->phase());
return true;
}
else
{
// standard event processing
return QObject::eventFilter(obj, event
);
}
}
class MouseEventFilter : public QObject
Q_OBJECT
public:
MouseEventFilter(QObject *parent = nullptr);
protected:
bool eventFilter(QObject *obj, QEvent *event) override;
MouseEventFilter::MouseEventFilter(QObject *parent) :
QObject(parent)
bool MouseEventFilter::eventFilter(QObject *obj, QEvent *event)
// The event is always eaten but the scroll is still active
if (event->type() == QEvent::Wheel)
QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event);
qDebug("Ate key press %d", wheelEvent->phase());
return true;
// standard event processing
return QObject::eventFilter(obj, event);
To copy to clipboard, switch view to plain text mode
Qt Code:
Switch view
this->myView->installEventFilter(new MouseEventFilter(this));
this->myView = new QGraphicsView(parent);
this->myView->installEventFilter(new MouseEventFilter(this));
To copy to clipboard, switch view to plain text mode
How do you know? Have you run your code in the debugger to see if your event filter is called with any event at all?
Have you disabled the scroll bars in the view (QAbstractScrollArea::setVerticalScrollBarPolicy(), QAbstractScrollArea::setHorizontalScrollBarPolicy())?
Yes, I have used the debugger. The event is called and it has no effect.
Right, I have disabled both scrollbars.
Qt Code:
Switch view
this
->myView
->setViewportUpdateMode
(QGraphicsView::BoundingRectViewportUpdate);
this->myView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->myView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->myView->centerOn(0, 0);
this->myView->setFixedWidth(110);
this->myView->setFocusPolicy(Qt::NoFocus);
this->myView->installEventFilter(new MouseEventFilter(this));
this->myView = new QGraphicsView(this->myScene);
this->myView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
this->myView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->myView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
this->myView->centerOn(0, 0);
this->myView->setFixedWidth(110);
this->myView->setDragMode(QGraphicsView::NoDrag);
this->myView->setFocusPolicy(Qt::NoFocus);
this->myView->installEventFilter(new MouseEventFilter(this));
To copy to clipboard, switch view to plain text mode