python - 无法从 QWidget 更改 QSlider 的值

标签 python qt pyqt pyqt5

我试图从另一个 QWidget 更改 QWidget 的值,但没有任何反应。我使用这一行从每个 Filter 对象调用 changeValue 函数:

self.filter1.valueChanged.connect(self.filter1.changeValue)

我已将 print(value) 语句放入 changeValue 方法中,以查看该方法是否正在被调用,但事实并非如此。

这是我的代码:

import sys
import numpy as np
import cv2
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtWidgets import (QWidget, QLabel, QHBoxLayout, QVBoxLayout,
                             QApplication, QPushButton, QSlider,
                             QFileDialog)


class Filter(QSlider):
    """Common base for all filters"""
    defaultK = 3
    filterCount = 0

    def __init__(self):
        super(Filter, self).__init__()

        # Variable for the constant of the OpenCV filter
        self.k = 3

        # Label for the slider
        self.k_lbl = QLabel(str(self.k))

        # Increase the number of filters created
        Filter.filterCount += 1

        # Slider for the first OpenCV filter, with min, max, default and step values
        self.thresh_sld = QSlider(Qt.Horizontal, self)
        self.thresh_sld.setFocusPolicy(Qt.NoFocus)
        self.thresh_sld.setMinimum(3)
        self.thresh_sld.setMaximum(51)
        self.thresh_sld.setValue(self.k)
        self.thresh_sld.setSingleStep(2)

    def changeValue(self, value):
        # Function for setting the value of k1

        print(value)

        if value % 2 == 1:
            self.k = value
        else:
            self.k = value + 1

        self.thresh_sld.setValue(self.k)
        self.k_lbl.setText(str(self.k))

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.filter1 = Filter()
        self.filter2 = Filter()

        # Function sending the slider signal to the processing function
        self.filter1.valueChanged.connect(self.filter1.changeValue)
        self.filter2.valueChanged.connect(self.filter2.changeValue)

        # Creates the main layout (vertical)
        v_main_lay = QVBoxLayout()
        # Adds the sliders and their labels to the bottom of the main layout
        v_main_lay.addWidget(self.filter1.k_lbl)
        v_main_lay.addWidget(self.filter1.thresh_sld)
        v_main_lay.addWidget(self.filter2.k_lbl)
        v_main_lay.addWidget(self.filter2.thresh_sld)

        # Sets the main layout
        self.setLayout(v_main_lay)

        # Sets the geometry, position, window title and window default mode
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Review')
        self.showMaximized()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

最佳答案

问题是您继承自QSlider,同时您在该类中创建一个名为 thresh_sld 的属性,这是在 View 中显示但未连接的元素对于changeValue,连接到ChangeValue的 slider 是Filter类Slider。

使用简单的解决方案是将属性 thresh_sld 的信号连接到提供的插槽,您必须更改以下内容:

self.filter1.valueChanged.connect(self.filter1.changeValue)
self.filter2.valueChanged.connect(self.filter2.changeValue)

至:

self.filter1.thresh_sld.valueChanged.connect(self.filter1.changeValue)
self.filter2.thresh_sld.valueChanged.connect(self.filter2.changeValue)

更优雅的解决方案是将 Filter 基类从 QSlider 更改为 QWidget,并将标签和 QSlider 添加到其布局中。

class Filter(QWidget):
    """Common base for all filters"""
    defaultK = 3
    filterCount = 0

    def __init__(self):
        super(Filter, self).__init__()
        lay = QVBoxLayout(self)

        # Variable for the constant of the OpenCV filter
        self.k = 3

        # Label for the slider
        self.k_lbl = QLabel(str(self.k))

        # Increase the number of filters created
        Filter.filterCount += 1

        # Slider for the first OpenCV filter, with min, max, default and step values
        self.thresh_sld = QSlider(Qt.Horizontal, self)
        self.thresh_sld.setFocusPolicy(Qt.NoFocus)
        self.thresh_sld.setMinimum(3)
        self.thresh_sld.setMaximum(51)
        self.thresh_sld.setValue(self.k)
        self.thresh_sld.setSingleStep(2)
        self.thresh_sld.valueChanged.connect(self.changeValue)

        lay.addWidget(self.k_lbl)
        lay.addWidget(self.thresh_sld)

    def changeValue(self, value):
        # Function for setting the value of k1

        print(value)

        if value % 2 == 1:
            self.k = value
        else:
            self.k = value + 1

        self.thresh_sld.setValue(self.k)
        self.k_lbl.setText(str(self.k))

class MainWindow(QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.filter1 = Filter()
        self.filter2 = Filter()

        # Creates the main layout (vertical)
        v_main_lay = QVBoxLayout()
        # Adds the sliders and their labels to the bottom of the main layout
        v_main_lay.addWidget(self.filter1)
        v_main_lay.addWidget(self.filter2)

        # Sets the main layout
        self.setLayout(v_main_lay)

        # Sets the geometry, position, window title and window default mode
        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Review')
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

关于python - 无法从 QWidget 更改 QSlider 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44102548/

相关文章:

python - PyQt5 中带有可点击文本的 QPlainTextEdit 小部件

python - 如何在 PyQt4 中创建 QString?

jquery - 从 jQuery 到 CherryPy 的 DELETE 请求不发送参数

python - 在正则表达式中选择破折号后的单词

c++ - QGraphicsRectItem 用鼠标 move 。如何?

qt - 如何使用诺基亚 Qt SDK 为桌面环境开发 Qt 应用程序?

python - SSL 请求失败 - Python OSX

python:如何将上下文传递给函数

c++ - 如何重置 VisualStudio(VS2010、VS2012)调试器缓存?

python - 使用 PyQt 和 matplotlib 在可滚动小部件中显示多个图