所以我使用Pyqt使用Python编程语言创建GUI程序。而我正试图在点击它时获得Qslider的价值,但我不明白如何......
paintComponent()
这是我在单击幻灯片时声明方法的方法,这是方法:
self.slide.mouseDoubleClickEvent= lambda event: self.slideclicked()
我希望得到鼠标点击的值,但我只是获得了Qslider的当前值。
答案 0 :(得分:1)
一种可能的解决方案是覆盖mouseDoubleClickEvent
方法并创建一个发送该信息的信号:
class Slider(QSlider):
pointClicked = pyqtSignal(QPoint)
def mouseDoubleClickEvent(self, event):
self.pointClicked.emit(event.pos())
class Widget(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
self.setLayout(QVBoxLayout())
self.slider = Slider()
self.layout().addWidget(self.slider)
self.slider.pointClicked.connect(lambda p: print(p.x(), p.y()))
如果您无法覆盖该方法,可以使用eventFilter
:
class ClickedHelper(QObject):
pointClicked = pyqtSignal(QPoint)
def __init__(self, widget, *args, **kwargs):
QObject.__init__(self, parent=widget)
self.obj = widget
self.obj.installEventFilter(self)
def eventFilter(self, obj, event):
if obj == self.obj and event.type() == QEvent.MouseButtonDblClick:
self.pointClicked.emit(event.pos())
return QObject.eventFilter(self, obj, event)
class Widget(QWidget):
def __init__(self, *args, **kwargs):
QWidget.__init__(self, *args, **kwargs)
self.setLayout(QVBoxLayout())
self.slider = QSlider()
self.layout().addWidget(self.slider)
helper = ClickedHelper(self.slider)
helper.pointClicked.connect(lambda p: print(p.x(), p.y()))