AttributeError: 'QSlider' object has no attribute 'subtractStep'

AttributeError: 'QSlider' object has no attribute 'subtractStep'

当我尝试 运行 此代码时出现错误:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'scaler_gui_3.ui'
#
# Created: Thu May 14 13:05:28 2015
#      by: PyQt4 UI code generator 4.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName(_fromUtf8("Form"))
        Form.resize(270, 219)
        self.gridLayout_2 = QtGui.QGridLayout(Form)
        self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2"))
        self.horizontalSliderFrames = QtGui.QSlider(Form)
        self.horizontalSliderFrames.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSliderFrames.setObjectName(_fromUtf8("horizontalSliderFrames"))
        self.gridLayout_2.addWidget(self.horizontalSliderFrames, 1, 0, 1, 1)
        self.gridLayout = QtGui.QGridLayout()
        self.gridLayout.setObjectName(_fromUtf8("gridLayout"))
        self.verticalScrollBarIW = QtGui.QScrollBar(Form)
        self.verticalScrollBarIW.setOrientation(QtCore.Qt.Vertical)
        self.verticalScrollBarIW.setObjectName(_fromUtf8("verticalScrollBarIW"))
        self.gridLayout.addWidget(self.verticalScrollBarIW, 0, 2, 1, 1)
        self.verticalLayout_2 = QtGui.QVBoxLayout()
        self.verticalLayout_2.setObjectName(_fromUtf8("verticalLayout_2"))
        self.labelMain = QtGui.QLabel(Form)
        self.labelMain.setObjectName(_fromUtf8("labelMain"))
        self.verticalLayout_2.addWidget(self.labelMain)
        self.horizontalScrollBarIW = QtGui.QScrollBar(Form)
        self.horizontalScrollBarIW.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalScrollBarIW.setObjectName(_fromUtf8("horizontalScrollBarIW"))
        self.verticalLayout_2.addWidget(self.horizontalScrollBarIW)
        self.gridLayout.addLayout(self.verticalLayout_2, 0, 0, 1, 1)
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem, 0, 1, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        self.horizontalLayout_2 = QtGui.QHBoxLayout()
        self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2"))
        self.gridLayout_2.addLayout(self.horizontalLayout_2, 6, 1, 1, 1)
        self.verticalLayout = QtGui.QVBoxLayout()
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.horizontalLayout_3 = QtGui.QHBoxLayout()
        self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
        self.labelSmallIW = QtGui.QLabel(Form)
        self.labelSmallIW.setObjectName(_fromUtf8("labelSmallIW"))
        self.horizontalLayout_3.addWidget(self.labelSmallIW)
        self.labelBigIW = QtGui.QLabel(Form)
        self.labelBigIW.setObjectName(_fromUtf8("labelBigIW"))
        self.horizontalLayout_3.addWidget(self.labelBigIW)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.horizontalLayout_3.addWidget(self.pushButton)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.gridLayout_2.addLayout(self.verticalLayout, 4, 0, 1, 1)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.toolButtonLoad = QtGui.QToolButton(Form)
        self.toolButtonLoad.setObjectName(_fromUtf8("toolButtonLoad"))
        self.horizontalLayout.addWidget(self.toolButtonLoad)
        self.prevButton = QtGui.QPushButton(Form)
        self.prevButton.setObjectName(_fromUtf8("prevButton"))
        self.horizontalLayout.addWidget(self.prevButton)
        self.nextButton = QtGui.QPushButton(Form)
        self.nextButton.setObjectName(_fromUtf8("nextButton"))
        self.horizontalLayout.addWidget(self.nextButton)
        self.gridLayout_2.addLayout(self.horizontalLayout, 2, 0, 1, 1)
        self.checkBox = QtGui.QCheckBox(Form)
        self.checkBox.setObjectName(_fromUtf8("checkBox"))
        self.gridLayout_2.addWidget(self.checkBox, 5, 0, 1, 1)

        self.retranslateUi(Form)
        QtCore.QObject.connect(self.prevButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.horizontalSliderFrames.subtractStep)
        QtCore.QObject.connect(self.nextButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.horizontalSliderFrames.addStep)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.labelMain.setText(_translate("Form", "TextLabel", None))
        self.labelSmallIW.setText(_translate("Form", "TextLabel", None))
        self.labelBigIW.setText(_translate("Form", "TextLabel", None))
        self.pushButton.setText(_translate("Form", "PushButton", None))
        self.toolButtonLoad.setText(_translate("Form", "...", None))
        self.prevButton.setText(_translate("Form", "<", None))
        self.nextButton.setText(_translate("Form", ">", None))
        self.checkBox.setText(_translate("Form", "CheckBox", None))

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    app.setStyle('cleanlooks')
    Form = QtGui.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

由于一些我不知道的"class related problems",其他人似乎也发生了类似的错误。我不明白为什么当我从 Geany 文本编辑器 "run" 时它似乎工作正常,但当我从终端使用 python my_gui.py 运行 它时却没有 我得到这个错误:

Traceback (most recent call last):
  File "gui_template.ui.py", line 136, in <module>
    ui = Ui_Form()
  File "gui_template.ui.py", line 39, in __init__
    self.setupUi(self)
  File "gui_template.ui.py", line 89, in setupUi
    QtCore.QObject.connect(self.prevButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.horizontalSliderFrames.subtractStep)
AttributeError: 'QSlider' object has no attribute 'subtractStep'

我也试过创建一个单独的 .py 文件,其方式与此人的做法类似:

https://youtu.be/FcX2FsPlVeI?t=10m25s

但是,我仍然遇到同样的错误...有什么想法吗?

addStepsubtractStep 插槽是 deprecated members of QSlider。它们只是为了提供与 Qt3 的向后兼容性。

看来您一定是在尝试 运行 使用不包含此类已弃用 API 的 Qt4/PyQt4 版本的示例脚本。

文档建议改用 setValue 插槽。但这需要传递适当的 increment/decrement 值,因此它并不是真正的直接替代品(特别是如果您通过 Qt Designer 连接信号)。

如果你想修复这个例子,你必须用类似的东西替换两个 signal/slot 连接:

    def setSlider(delta):
        self.horizontalSliderFrames.setValue(
            self.horizontalSliderFrames.value() +
            delta * self.horizontalSliderFrames.singleStep())

    self.prevButton.clicked.connect(lambda: setSlider(-1))
    self.nextButton.clicked.connect(lambda: setSlider(+1))