D
D
Daniel Nakushnov2016-02-20 02:40:08
Python
Daniel Nakushnov, 2016-02-20 02:40:08

Why doesn't PyQt5 consider the QObject widget class?

Gives the following error:

Traceback (most recent call last):
File "C:\Python34\ORGHaniser.py", line 29, in
widget = ORGH_widget()
File "C:\Python34\ORGHaniser.py", line 7, in __init__
self.maindialog = ORGH_app(None)
File "C:\Python34\ORGHaniser.py", line 17, in __init__
Ui_Form.setupUi(self, ORGH_app)
File "C:\Python34\ORGHaniser_ui.py", line 5, in setupUi
Form.setObjectName( "Form")
TypeError: QObject.setObjectName(str): first argument of unbound method must have type 'QObject'

The code:
from PyQt5 import QtCore, QtGui, QtWidgets
from ORGHaniser_ui import Ui_Form
import sys
class ORGH_widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(ORGH_widget, self).__init__(parent)
        self.maindialog = ORGH_app(None)
        self.setMainWidget(self.maindialog)
        self.maindialog.show()
        self.exec_loop()
    def keyPressEvent(self, e):
        if e.key() == Qt.Key_Escape:
            self.close()

class ORGH_app(Ui_Form, QtWidgets.QMainWindow):
    def __init__(self,parent):
        Ui_Form.setupUi(self, ORGH_app)
        self._connectSlots()
    def _connectSlots(self):
        Ui_Form.pushButton.clicked.connect(self.lineedit,self._slotAddClicked)
    def _slotAddClicked(self):
        text = self.lineedit.text()
        if len(text):
            tvi = QTableViewItem(self.tableview)
            tvi.setText(0,text)
            self.lineedit.clear()
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    widget = ORGH_widget()
    sys.exit(app.exec())

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"))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2016-02-20
@DanielDan0

In general, you have a very strange organization of classes, hence the errors. Also, you have errors in the source texts, for example, lineedit and lineEdit, where did QTableViewItem come from, it is not even in the library, where did the exec_loop method come from.
When giving an example, be careful.
And your problem is easier to solve like this:

class ORGH_widget(Qt.QWidget):

    def __init__(self, parent=None):
        super(ORGH_widget, self).__init__(parent)
        self.maindialog = ORGH_app(None)
        self.layout = Qt.QVBoxLayout(self)
        self.layout.addWidget(self.maindialog)
        self.maindialog.show()
#        self.exec_loop()

    def keyPressEvent(self, e):
        if e.key() == Qt.Qt.Key_Escape:
            self.close()


class ORGH_app(Qt.QMainWindow):

    def __init__(self, parent):
        super(ORGH_app, self).__init__()
        self.form = Ui_Form()
        self.form.setupUi(self)

        self._connectSlots()

    def _connectSlots(self):
        self.form.pushButton.clicked.connect(self._slotAddClicked)

    def _slotAddClicked(self):
        text = self.form.lineEdit.text()
        if len(text):
#             tvi = Qt.QTableViewItem(self.form.tableView)
#             tvi.setText(0, text)
            self.form.lineEdit.clear()

if __name__ == "__main__":
    app = Qt.QApplication(sys.argv)
    main = ORGH_app(None)
    main.show()
    sys.exit(app.exec())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question