4

In PyQtGraph you can zoom into the plots with the scroll wheel. However, when embedding PyQtGraph inside a QScrollArea, scrolling both zooms into the hovered plot AND scrolls the QScrollArea.

Example GIF

Minimal reproducable code:

from PyQt5.QtWidgets import QApplication, QScrollArea, QMainWindow
import pyqtgraph as pg

app = QApplication([])
window = QMainWindow()
scroll = QScrollArea(window)
window.setCentralWidget(scroll)

frame = pg.GraphicsLayoutWidget()
plot = frame.addPlot().plot([1,2,3], [2,5,10])

scroll.setWidget(frame)
scroll.setWidgetResizable(True)

frame.setFixedHeight(600)
window.setFixedHeight(500)
window.show()
app.exec_()

I tried googling the issue, and I found a way to stop panning and allow scrolling, however I want it the other way around: prevent scrolling and allow panning.

Is it possible inside PyQtGraph to stop QScrollArea scrolling when a plot is hovered?

kangalio
  • 652
  • 8
  • 16
  • Oh yea, sorry. I'll add it to the question as soon as I've finished it. EDIT: Dinner is ready, it'll take a while, sorry :/ – kangalio Sep 04 '19 at 17:24

1 Answers1

0

The solution is to cancel the wheelEvent of QScrollArea:

from PyQt5.QtWidgets import QApplication, QScrollArea, QMainWindow
import pyqtgraph as pg


class ScrollArea(QScrollArea):
    def wheelEvent(self, event):
        pass


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    window = QMainWindow()
    scroll = ScrollArea(window)
    window.setCentralWidget(scroll)

    frame = pg.GraphicsLayoutWidget()
    plot = frame.addPlot().plot([1, 2, 3], [2, 5, 10])

    scroll.setWidget(frame)
    scroll.setWidgetResizable(True)

    frame.setFixedHeight(600)
    window.setFixedHeight(500)
    window.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • I realize I didn't explicitly state this in the original question, but is it possible to stop scrolling _only_ when there is a plot hovered? When the mouse is over an empty area it would be nice to still have Qt scrolling – kangalio Sep 04 '19 at 18:38
  • @kangalioo Post a new question pointing this out and provide an appropriate MRE to help you. :-) – eyllanesc Sep 04 '19 at 18:39
  • I guess you're right :p Though it feels kinda wrong to ask pretty much the same question immediately after, just with a little change. So I'm gonna wait with that till I feel the absolute necessity for it – kangalio Sep 04 '19 at 18:43
  • 1
    @kangalioo This is how SO works, your question is to avoid scrolling in all cases, and my answer does. If you add another requirement then it is another different question even if it resembles the first and deserves a new post. – eyllanesc Sep 04 '19 at 18:46
  • Tried the exact code you provided, doesn't work for me, the wheel is still zooming in/out.. – kalzso Aug 13 '20 at 08:57
  • @kalzso Have you correctly understood the objective of the OP? By default, when the mouse wheel is used, 2 processes happen: zoom (in / out) + scrollbar scroll and the OP does not want the second to happen. And that's what you indicate, I think your goal is another, – eyllanesc Aug 13 '20 at 13:52