[PyQt] Signal and slot not always working

s.achterop at rug.nl s.achterop at rug.nl
Wed Mar 29 18:48:51 BST 2017


    Hello list,

I have some problems to get signals/slots to work.
Simple examples work, but in an example using QtDesigner generated code the slot never is called.
In the code below you find the "connect" method called in two places.
As is, the program DOES NOT work, in the sense that it does not end when the button is pressed.

When the other "connect", now comment, is used, clicking of the button DOES end the program.
The question is of course, what am I doing wrong?

Thanks in advance:

================= main.py
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QMainWindow
from PyQt5.QtCore import qDebug, QCoreApplication, pyqtSignal, pyqtSlot

from ui import Ui_Widget

class MyMainWindow(QMainWindow, Ui_Widget):
     def __init__(self):
         super().__init__()
         self.ui = Ui_Widget()
         self.ui.setupUi(self)
# doesn't work!
         self.ui.pushButton.clicked.connect(QCoreApplication.instance().quit)


if __name__ == "__main__":
     app = QApplication(sys.argv)
     MainWindow = QMainWindow()
     ui = MyMainWindow()
     ui.setupUi(MainWindow)
     MainWindow.show()
     sys.exit(app.exec_())

===========  ui.y
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Widget(object):
     def setupUi(self, Widget):
	Widget.setObjectName("Widget")
	Widget.resize(400, 300)
	self.pushButton = QtWidgets.QPushButton(Widget)
         self.pushButton.setGeometry(QtCore.QRect(160, 60, 87, 27))
         self.pushButton.setObjectName("pushButton")

         self.retranslateUi(Widget)
         QtCore.QMetaObject.connectSlotsByName(Widget)
# this one does work!
#        self.pushButton.clicked.connect(QtCore.QCoreApplication.instance().quit)

     def retranslateUi(self, Widget):
	_translate = QtCore.QCoreApplication.translate
	Widget.setWindowTitle(_translate("Widget", "Widget"))
	self.pushButton.setText(_translate("Widget", "PushButton"))
======= end of programs


More information about the PyQt mailing list