Reputation: 21
I am using qslider in python 3. I can move cursor forward and backward by keyboard event of up, down, left and right arrow. I would like to disable specifically part of them: up and down arrow move cursor while right and left cursor do not. Is it possible to do that?
Upvotes: 2
Views: 709
Reputation: 244291
You have to override the keyPressEvent method:
from PyQt5 import QtCore, QtGui, QtWidgets
class Slider(QtWidgets.QSlider):
def keyPressEvent(self, event):
if event.key() in (QtCore.Qt.Key_Left, QtCore.Qt.Key_Right):
return
super(Slider, self).keyPressEvent(event)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QWidget()
lay = QtWidgets.QHBoxLayout(w)
slider = Slider()
label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
slider.valueChanged.connect(label.setNum)
label.setNum(slider.value())
lay.addWidget(slider)
lay.addWidget(label)
w.resize(160, 240)
w.show()
sys.exit(app.exec_())
Upvotes: 4