D
D
Daniel Nakushnov2015-09-26 01:02:12
Python
Daniel Nakushnov, 2015-09-26 01:02:12

How to make a signal handler in PyQt5?

I have such a problem - signal handlers do not work correctly, they display an error.
The code:

class app(Ui_Form):
    def __init__(self,parent):
        Ui_Form.__init__(self)
        self._connectSlots()
    def _connectSlots(self):
        Ui_Form.pushButton.clicked.connect(self.lineedit, self._slotAddClicked)
    def _slotAddClicked(self):
        text = self.lineedit.text()
        if len(text):
            lvi = QTableViewItem(self.tableview)
            lvi.setText(0,text)
            self.lineedit.clear()

UI code:
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.tableView = QtWidgets.QTableView(Form)
        self.tableView.setGeometry(QtCore.QRect(10, 10, 381, 251))
        self.tableView.setObjectName("tableView")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(10, 270, 301, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.pushButton = QtWidgets.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(320, 270, 75, 23))
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.lineEdit.setText(_translate("Form", "USER */%/# +n/-n"))
        self.pushButton.setText(_translate("Form", "OK"))

Throws AttributeError: type object 'Ui_Form' has no attribute 'pushButton'.
Please tell me how to do it right? Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail, 2015-09-26
@DanielDan0

pushButton is created inside the setupUi method. You need to call it in the app class constructor before calling self._connectSlots()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question