Results 1 to 7 of 7

Thread: Disable Scroll in QGraphicsview

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2012
    Posts
    10
    Thanks
    1

    Default Disable Scroll in QGraphicsview

    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?

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    454
    Thanks
    10
    Thanked 60 Times in 58 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Disable Scroll in QGraphicsview

    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

  3. #3
    Join Date
    Aug 2012
    Posts
    10
    Thanks
    1

    Default Re: Disable Scroll in QGraphicsview

    Qt Code:
    1. class MyClass : public QGraphicsScene
    2. {
    3. public:
    4. MyClass(QObject *parent = nullptr);
    5. bool eventFilter(QObject *obj, QEvent *event) override;
    6. };
    7.  
    8. MyClass::MyClass(QObject *parent) :
    9. {
    10. this->installEventFilter(this);
    11. }
    12.  
    13.  
    14. bool MyClass::eventFilter(QObject *obj, QEvent *event)
    15. {
    16. if (event->type() == QEvent::GraphicsSceneWheel)
    17. {
    18. // QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event);
    19. return true;
    20. }
    21. else
    22. {
    23. // standard event processing
    24. return QObject::eventFilter(obj, event);
    25. }
    26. }
    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.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,719
    Thanks
    259
    Thanked 759 Times in 749 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Disable Scroll in QGraphicsview

    I have installed an event filter in my inherited class "Myclass". But it has no effect.
    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Aug 2012
    Posts
    10
    Thanks
    1

    Default Re: Disable Scroll in QGraphicsview

    I have installed an event filter on QGraphicView but it also has no effect.

    Qt Code:
    1. class MouseEventFilter : public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. MouseEventFilter(QObject *parent = nullptr);
    7.  
    8. protected:
    9. bool eventFilter(QObject *obj, QEvent *event) override;
    10. };
    11.  
    12. MouseEventFilter::MouseEventFilter(QObject *parent) :
    13. QObject(parent)
    14. {
    15. }
    16.  
    17. bool MouseEventFilter::eventFilter(QObject *obj, QEvent *event)
    18. {
    19. // The event is always eaten but the scroll is still active
    20. if (event->type() == QEvent::Wheel)
    21. {
    22. QWheelEvent *wheelEvent = static_cast<QWheelEvent*>(event);
    23. qDebug("Ate key press %d", wheelEvent->phase());
    24. return true;
    25. }
    26. else
    27. {
    28. // standard event processing
    29. return QObject::eventFilter(obj, event);
    30. }
    31. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. this->myView = new QGraphicsView(parent);
    2. this->myView->installEventFilter(new MouseEventFilter(this));
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,719
    Thanks
    259
    Thanked 759 Times in 749 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Disable Scroll in QGraphicsview

    I have installed an event filter on QGraphicView but it also has no effect.
    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())?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Aug 2012
    Posts
    10
    Thanks
    1

    Default Re: Disable Scroll in QGraphicsview

    Yes, I have used the debugger. The event is called and it has no effect.
    Right, I have disabled both scrollbars.

    Qt Code:
    1. this->myView = new QGraphicsView(this->myScene);
    2. this->myView->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    3. this->myView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    4. this->myView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    5. this->myView->centerOn(0, 0);
    6. this->myView->setFixedWidth(110);
    7. this->myView->setDragMode(QGraphicsView::NoDrag);
    8. this->myView->setFocusPolicy(Qt::NoFocus);
    9. this->myView->installEventFilter(new MouseEventFilter(this));
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to disable scroll bars in QPrintPreviewWidget?
    By manu_maithri in forum Newbie
    Replies: 1
    Last Post: 21st January 2014, 02:01
  2. Qt Creator how to disable two finger scroll rubber band
    By Markus in forum Qt Tools
    Replies: 0
    Last Post: 6th April 2012, 00:01
  3. How to Disable Scroll Bar of QGraphicsView (or QWidget) ??
    By jiapei100 in forum Qt Programming
    Replies: 5
    Last Post: 6th November 2011, 04:45
  4. Disable QListView scroll event
    By migel in forum Newbie
    Replies: 1
    Last Post: 27th May 2011, 18:32
  5. How to disable vertical scroll bar in QTableWidget
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2009, 12:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.