简体   繁体   English

pyQt5:无法连接 QSpinBox::valueChanged(int)

[英]pyQt5: Cannot connect QSpinBox::valueChanged(int)

I am new to Python and Qt.我是 Python 和 Qt 的新手。 Currently, I am trying to build the UI for a larger application, but I am running into problems regarding signals and slots.目前,我正在尝试为更大的应用程序构建 UI,但我遇到了有关信号和插槽的问题。

Here is my code:这是我的代码:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QObject, pyqtSlot
import sys


class Ui_configDialog(QtWidgets.QDialog):
    def __init__(self):
        super(Ui_configDialog, self).__init__()

        self.setupUi()

    def setupUi(self):
        self.setObjectName("configDialog")
        self.setWindowModality(QtCore.Qt.WindowModal)
        self.resize(425, 380)

        row1 = DataRow(self)

        self.show()


class DataRow:
    def __init__(self, dialog):
        rect = QtCore.QRect(10, 40, 91, 30)

        self.text_fRep = QtWidgets.QSpinBox(dialog)
        self.text_fRep.setGeometry(rect.translated(100, 0))
        self.connect_signal()

    @pyqtSlot(int)
    def fRep_changed(self, value):
        print(value)

    def connect_signal(self):
        self.text_fRep.valueChanged.connect(self.fRep_changed)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    dialog = Ui_configDialog()
    sys.exit(app.exec_())

What I am trying to achieve is, that the slot fRep_changed is called whenever the value of the QSpinBox object is changed.我想要实现的是,每当 QSpinBox 对象的值发生更改时,都会调用插槽 fRep_changed。 But with this code I receive a compile error:但是使用此代码我收到一个编译错误:

QObject::connect: Cannot connect QSpinBox::valueChanged(int) to (null)::fRep_changed(int)
TypeError: connect() failed between valueChanged(int) and fRep_changed()

I cannot see, why I shouldn't be able to connect the signal to the slot.我看不到,为什么我不能将信号连接到插槽。

I also removed the @pyqtSlot(int).我还删除了@pyqtSlot(int)。 The application starts, but nothing happens when the value is changed.应用程序启动,但更改值时没有任何反应。

Thank you for your help in advance!提前谢谢你的帮助!

Your code has 2 errors, the first one is that the slots are only implemented within the classes that inherit from QObject , so the easiest thing is for your class to inherit from QObject .您的代码有 2 个错误,第一个是插槽仅在从QObject继承的类中实现,因此最简单的方法是让您的类从QObject继承。 The second you will see after making the previous change, it will happen that even if you change the value of QSpinBox will never be called to the slot, and this happens because the collector deletes the object of row1 of the DataRow class, the solution is simple, you just have to make row a member of the class through self, ie change row1 by self.row1上次修改后你会看到的第二个情况,即使你改变了QSpinBox的值也永远不会被调用到slot上,这是因为收集器删除了DataRow类的row1的对象,解决方法是简单,你只需要通过self让row成为类的成员,即通过self.row1改变row1

class Ui_configDialog(QtWidgets.QDialog):
    def __init__(self):
        super(Ui_configDialog, self).__init__()

        self.setupUi()

    def setupUi(self):
        self.setObjectName("configDialog")
        self.setWindowModality(QtCore.Qt.WindowModal)
        self.resize(425, 380)

        self.row1 = DataRow(self)

        self.show()


class DataRow(QObject):
    def __init__(self, dialog, parent=None):
        QObject.__init__(self, parent)

        rect = QtCore.QRect(10, 40, 91, 30)

        self.text_fRep = QtWidgets.QSpinBox(dialog)
        self.text_fRep.setGeometry(rect.translated(100, 0))
        self.connect_signal()

    @pyqtSlot(int)
    def fRep_changed(self, value):
        print(value)

    def connect_signal(self):
        self.text_fRep.valueChanged.connect(self.fRep_changed)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM