0

I want to be able to detect if the slider has moved to a new position and then do some actions in another function called sl. Here is my code:


import sys
from PyQt5 import QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QLabel, QLineEdit, QScrollBar, QMainWindow, QMenu, QWidget, QGridLayout, \
  QMessageBox, QListWidget, QPlainTextEdit, QTableWidget, QTableWidgetItem, QHeaderView
from PyQt5.QtWidgets import QMenuBar, QHBoxLayout, QVBoxLayout, QSlider, QPushButton, QDial, QBoxLayout, QSpacerItem
from PyQt5.QtGui import QFont, QColor, QPixmap, QResizeEvent, QPen
import PyQt5.QtGui as QtGui
import PyQt5.QtCore as QtCore

class ControlWidget(QWidget):

  def __init__(self):

    QWidget.__init__(self)
    self.layout = QVBoxLayout()
    self.layout.setContentsMargins(10, 10, 10, 10)

    widget3 = QLabel("Manual")
    big_font = QFont('Arial')
    big_font.setPointSize(15)
    widget3.setFont(big_font)
    self.layout.addWidget(widget3, stretch=1)

    widget2 = ManualWidget()
    self.layout.addWidget(widget2, stretch=4)

    self.setLayout(self.layout)


class ManualWidget(QWidget):

  def __init__(self):
    QWidget.__init__(self)

    self.layout = QHBoxLayout()

    right_widget = QWidget()
    right_layout = QVBoxLayout()

    right_top_widget = QWidget()
    self.sld1 = self.slider('V1:', 0, 100)

    right_top_layout = QVBoxLayout()
    right_top_layout.setContentsMargins(0, 0, 0, 0)
    right_top_layout.addWidget(self.sld1, stretch=1)
    right_top_widget.setLayout(right_top_layout)
    right_layout.addWidget(right_top_widget)

    right_widget.setLayout(right_layout)
    self.layout.addWidget(right_widget, stretch=1)

    self.setLayout(self.layout)
    self.layout.addWidget(self.name)
    self.layout.addWidget(self.label)
    self.layout.addWidget(self.slider)
    print(self.sl())

  def slider(self, name, low, high, step=10):
    self.name = QLabel(str(name))
    self.label = QLabel(str(low))

    self.slider = QSlider(Qt.Horizontal, self)
    self.slider.setMinimum(low)
    self.slider.setMaximum(high*step)
    self.slider.setValue(low)
    self.slider.valueChanged.connect(self.change_value)
    self.action = False
    self.slider.sliderMoved.connect(self.act1)

  def change_value(self):
    self.set_point = (float(self.slider.value())) / 10
    self.label.setText(str(self.set_point))

  def act1(self):
    self.action = True
    return self.action

  def sl(self):
    if self.action == True:
      x = 3
    else:
      x = 6
    return x

class MainWidget(QWidget):

  def __init__(self):
    QWidget.__init__(self)
    self.layout = QVBoxLayout()
    self.layout.setContentsMargins(0, 0, 0, 0)

    big_font = QFont('Arial')
    big_font.setPointSize(10)

    bottom_widget = ControlWidget()
    self.layout.addWidget(bottom_widget, stretch=10)
    self.setLayout(self.layout)


class Window(QMainWindow):

  def __init__(self, widget):
    QMainWindow.__init__(self)
    self.setWindowTitle("Flow test rig")
    self.menu = self.menuBar()
    self.setCentralWidget(widget)
    self.status = self.statusBar()
    widget.parent = self


app = QApplication(sys.argv)
main_widget = MainWidget()
win = Window(main_widget)

win.show()
sys.exit(app.exec_())

I tried to detect it with the act1 function, however, self.action is always False, or when it becomes True, it does not reset to False after the first move of the slider. I appreciate it if someone would help me.

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
lp roj
  • 1
  • 2
  • First of all, remove that `__init__` in `slider()`, because it's just ***wrong***. Also the assignment of `self.sld1` is useless, since `slider()` doesn't return anything. That said, your question is confusing: `self.sl` is never called, nor it's clear how/when/why should it be called, and it's also unclear why you connected to both `valueChanged` and `sliderMoved` and what should be the difference for what you need. Please take your time to rephrase your question and ensure that it can be clear to others. – musicamante Jan 05 '23 at 14:32
  • I will use x somewhere else in my code, I had removed that part to avoid complexity. I just simply want to detect slider move (as clear as this) and send a True value when it is moved and reset this value to False before the next move. I am also using self.sld1 somewhere else in the code. @musicamante – lp roj Jan 05 '23 at 14:37
  • If [`tracking`](https://doc.qt.io/qt-5/qabstractslider.html#tracking-prop) is enabled (the default), `valueChanged` behaves exactly like `sliderMoved`, meaning that there's no difference or distinction between the beginning or end of a "movement". Depending on your needs, you might need to use the other signals, or eventually consider QTimer based checks. Remember that return values of functions connected to signals are completely ignored, so that `x` is useless, and you must make it an instance attribute if you need to use it later. Finally, don't overwrite existing members (`widget.parent`). – musicamante Jan 05 '23 at 15:02

0 Answers0