Search code examples
pythonscrollpyqtpyqt5pyqtgraph

pyqtgraph disable parent widget scrolling while mouse hovering on/is used from sub-widget


I have a QTreeWidget which I have populated with two sub-widgets a pyqtgraph.TableWidget() and a pyqtgraph.ImageView(). Now the issue that I have is that when my mouse is within the ImageView it is used to apply different actions (e.g. zoom in/out, etc) but this affects the scrolling of the QTreeWidget. What I would like to do is to disable the scrolling of the QTreeWidget when my mouse is within (hovering?) the ImageView boundary and is used for the ImageView functionality. Is that possible?

Here is some code snippet of my widget structure:

# Tree
self.tree_widget = QtWidgets.QTreeWidget()
self.tree_widget.setHeaderLabels(["Key", "Value", "Image"])
root_item = QtWidgets.QTreeWidgetItem(["Test Item"])
self.tree_widget.addTopLevelItem(root_item)

# Data
val=numpy.random.normal(size=(32, 24, 3))

# Sub-widgets
tableWidget = pyqtgraph.TableWidget(sortable=False)
tableWidget.setData(val)

im1 = pyqtgraph.ImageView()
im1.setImage(val.T)

self.tree_widget.setItemWidget(root_item, 1, tableWidget)
self.tree_widget.setItemWidget(root_item, 2, im1)

Solution

  • It's not very clear what mouse events you're having issues with, as the only thing I could think of is wheel events.

    If that's the case, just create a subclass of ImageViewer and set as accepted all wheel events it receives: in this way, ImageViewer will work as expected but wheel events won't be propagated to the parent widget (the tree):

    class MyImageView(pyqtgraph.ImageView):
        def wheelEvent(self, event):
            event.accept()
    
        # you can do the same with other mouse events, if that's your issue too
        def mousePressEvent(self, event):
            event.accept()
    
    class YourWidget(QtWidgets.QWidget):
        def __init__(self, *args, **kwargs):
            QtWidgets.QWidget.__init__(self, *args, **kwargs)
            [...]
            im1 = MyImageView()