Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'StartQT4' object has no attribute 'accept'

Tags:

python

qt

I'm trying to program a quick dialog with QT4 and Python. I've generated the Python class, using pyuic4, and tried to make a small python script to start it up:

import sys
from PyQt4 import QtCore, QtGui
from ConfigGUI import Ui_ConfigGUI

class StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_ConfigGUI()
        self.ui.setupUi(self)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

When I try to run it, it says AttributeError: 'StartQT4' object has no attribute 'accept'.

What did I do wrong?

like image 341
Numeri Avatar asked May 11 '26 09:05

Numeri


1 Answers

I managed to reproduce your problem. You selected a form based on dialogs in the QtDesigner, but are trying to construct it inside a QMainWindow.

Form base selection dialog in QtDesigner

The UI code tries to bind its buttons to default dialog slots acceptand reject which are not available in a QMainWindow.

From ConfigGUI.py:

QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("accepted()")), Dialog.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL(_fromUtf8("rejected()")), Dialog.reject)

The class contains a method called setupUi(). This takes a single argument which is the widget in which the user interface is created. The type of this argument (typically QDialog, QWidget or QMainWindow) is set in Designer. We refer to this type as the Qt base class.

-- http://pyqt.sourceforge.net/Docs/PyQt4/designer.html

So, either select Main Window in the Designer as a base class, or change the inheritance of StartQT4 to QtGui.QDialog.

like image 182
Cilyan Avatar answered May 12 '26 23:05

Cilyan