Mouse events in QGraphicsItem

85 views
Skip to first unread message

Kevin Miao

unread,
May 3, 2017, 1:17:26 AM5/3/17
to pyqtgraph
I'm trying to embed a PlotWidget in a QGraphicsItem, but it seems like mousePress/Move/Release events are being consumed by the container widget and not forwarded to the plot (but hover and scroll events still work as expected). This breaks all click-based functionality of the plot. If I just embed the PlotWidget directly using QGraphicsProxyWidget (and without a container QWidget), then mouse events function correctly. Is there anything that I'm missing in getting the mouse events to register when a PlotWidget is in a embedded QWidget container?

The following snippet should demonstrate the problem:

from PyQt5 import QtWidgets, QtCore, QtGui
import pyqtgraph as pg
import numpy as np

def main():
    app = QtWidgets.QApplication([])
    view = QtWidgets.QGraphicsView()
    scene = QtWidgets.QGraphicsScene(parent=view)
    view.setScene(scene)

    plot = pg.PlotWidget()
    curve = plot.plot()

    # uncomment to disable PlotWidget click functionality

    # container_widget = QtWidgets.QWidget()
    # layout = QtWidgets.QHBoxLayout(container_widget)
    # layout.addWidget(plot)
    # layout.setContentsMargins(0, 0, 0, 0)
    # container_widget.setLayout(layout)
    # plot = container_widget

    xs = list()
    ys = list()
    idx = 0

    def update():
        nonlocal idx
        curve.setData(x=xs, y=ys)
        xs.append(idx)
        ys.append(np.sin(idx / 20))
        idx += 1
        return

    proxy = QtWidgets.QGraphicsProxyWidget()
    proxy.setWidget(plot)
    proxy.setGeometry(QtCore.QRectF(0, 0, 600, 600))

    x, y, w, h = plot.x(), plot.y(), plot.width(), plot.height()

    border_size = 10

    container = QtWidgets.QGraphicsRectItem()
    container.setPen(QtGui.QPen(QtGui.QColor(0, 0, 0)))
    container.setBrush(QtGui.QBrush(QtGui.QColor(100, 100, 100)))
    container.setRect(x - border_size, y - border_size, w + 2 * border_size, h + 2 * border_size)

    proxy.setParentItem(container)
    scene.addItem(container)

    timer = QtCore.QTimer()
    timer.timeout.connect(update)
    timer.start(10)

    view.resize(800, 800)
    view.show()
    app.exec_()

if __name__ == '__main__':
    main()



Reply all
Reply to author
Forward
0 new messages