horizo​​ntalSilder(PyQt5):事件取决于位置|起始位置取决于过程

时间:2019-02-14 13:53:54

标签: python slider pyqt5

我对PyQt5上的horizo​​ntalSlider有疑问。
我想让我的滑块根据位置是1还是0进行操作。因此1个startprocess和0个stop过程。 这是我的代码:

  self.horizontalSlider = QtWidgets.QSlider(Form)
        self.horizontalSlider.setGeometry(QtCore.QRect(190, 50, 51, 41))
        self.horizontalSlider.setStyleSheet("QSlider::groove:horizontal { \n"
"    background-color: #bdc3c7;\n"
"    border: 0px solid #424242; \n"
"    height: 18px; \n"
"    border-radius: 8px;\n"
"}\n"
"\n"
"QSlider::handle:horizontal { \n"
"    background-color: #7f8c8d; \n"
"    border: 2px solid #7f8c8d; \n"
"    width: 16px; \n"
"    height: 20px; \n"
"    line-height: 20px; \n"
"    margin-top: -5px; \n"
"    margin-bottom: -5px; \n"
"    border-radius: 10px; \n"
"}\n"
"\n"
"QSlider::handle:horizontal:hover { \n"
"    border-radius: 10px;\n"
"}\n"
"\n"
"QSlider::sub-page:Horizontal { \n"
"background-color: #27ae60;\n"
"border : 0px solid;\n"
"border-radius: 8px;\n"
"}\n"
"\n"
"QSlider::add-page:Horizontal {\n"
" background-color: #e74c3c;\n"
"border : 0px solid;\n"
"border-radius: 8px;\n"
" }")
        self.horizontalSlider.setMaximum(1)
        self.horizontalSlider.setPageStep(1)
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setInvertedAppearance(False)
        self.horizontalSlider.setInvertedControls(False)
        self.horizontalSlider.setTickPosition(QtWidgets.QSlider.NoTicks)
        self.horizontalSlider.valueChanged.connect(self.call_deadlineslave)

# There is more code between

def press_key_event(self, event):
        if event.key()==Qt.Key_Right:
            self.horizontalSlider.setValue(self.horizontalSlider.value()+1)
        elif event.key()==Qt.Key_Left:
            self.horizontalSlider.setValue(self.horizontalSlider.value()-1)
        else :
            QtWidgets.press_key_event(self, event)

    def call_deadlineslave(self):
        commands.enable_deadlineslave()

第二件事是开始位置。如果启用了从属设备,可以使滑块从右开始启动,如果禁用从属设备,则可以从左开始?就像如果Dealineslave.exe运行滑块从右开始?

非常感谢,
最好,
Pixidream

2 个答案:

答案 0 :(得分:0)

要使滑块根据其位置触发不同的行为,可以使用.valueChanged信号-您已经在此处连接了它:

self.horizontalSlider.valueChanged.connect(self.call_deadlineslave)

接收槽self.call_deadlineslave将接收新值,例如

def call_deadlineslave(self, value):
    if value == 1:
        commands.enable_deadlineslave()
    else:
        commands.disable_deadlineslave()

要确定启动时的起始值,可以在创建滑块时使用.setValue。它会在显示之前更新,因此会从您喜欢的任何位置开始。

self.horizontalSlider.setValue(1)

self.horizontalSlider.setValue(0)

另外一个注释,您在此处设置值+1:

self.horizontalSlider.setValue(self.horizontalSlider.value()+1)

...但是滑块限制为0..1,因此您最好设置:

self.horizontalSlider.setValue(1)

答案 1 :(得分:0)

感谢@mfitzp给您的答案!
我可以使用以下代码解决问题:

self.horizontalSlider = QtWidgets.QSlider(self.centralwidget)
        self.horizontalSlider.setGeometry(QtCore.QRect(60, 30, 51, 41))
        self.horizontalSlider.setStyleSheet("QSlider::groove:horizontal { \n"
"    background-color: #bdc3c7;\n"
"    border: 0px solid #424242; \n"
"    height: 18px; \n"
"    border-radius: 8px;\n"
"}\n"
"\n"
"QSlider::handle:horizontal { \n"
"    background-color: #7f8c8d; \n"
"    border: 2px solid #7f8c8d; \n"
"    width: 16px; \n"
"    height: 20px; \n"
"    line-height: 20px; \n"
"    margin-top: -5px; \n"
"    margin-bottom: -5px; \n"
"    border-radius: 10px; \n"
"}\n"
"\n"
"QSlider::handle:horizontal:hover { \n"
"    border-radius: 10px;\n"
"}\n"
"\n"
"QSlider::sub-page:Horizontal { \n"
"background-color: #27ae60;\n"
"border : 0px solid;\n"
"border-radius: 8px;\n"
"}\n"
"\n"
"QSlider::add-page:Horizontal {\n"
" background-color: #e74c3c;\n"
"border : 0px solid;\n"
"border-radius: 8px;\n"
" }")
        self.horizontalSlider.setMaximum(1)
        self.horizontalSlider.setPageStep(1)
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setInvertedAppearance(False)
        self.horizontalSlider.setInvertedControls(False)
        self.horizontalSlider.setTickPosition(QtWidgets.QSlider.NoTicks)
        self.horizontalSlider.setObjectName("horizontalSlider")
        self.horizontalSlider.valueChanged.connect(self.change_value)
    def change_value(self, value):
        if value == 1:
            self.call_deadlineslave()
        else:
            self.kill_deadlineslave()