I'm using the following code to call a function whenever the infinite line is dragged. It does not call the function.
import sys
import pyqtgraph as pg
from PyQt5.QtCore import pyqtSlot as Slot, Qt, QMetaObject
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class MyMainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.setGeometry(300, 300, 400, 300)
self.setWindowTitle('Hello World')
self.plot_widget = pg.PlotWidget()
self.plot_item = self.plot_widget.plot([1,0,2], pen='b', name='p0')
self.vline = pg.InfiniteLine(movable=True, angle=90)
self.vline.setObjectName('vline')
self.plot_widget.addItem(self.vline)
self.setCentralWidget(self.plot_widget)
QMetaObject.connectSlotsByName(self)
@Slot(object)
def on_vline_sigDragged(self, obj):
print('dragged')
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = MyMainWindow()
ui.show()
sys.exit(app.exec_())
But when I assign the slot manually via
self.vline.sigDragged.connect(self.on_vline_sigDragged)
it works fine.
Why is that so?
The connectSlotsByName function searches recursively for matching child objects, but
vlineisn't a descendant of the main window:So a simple fix would be to explicitly set the parent of the relevant object: