[PyQt] The 'connectSlotsByName' mechanism seems not working

Phil Thompson phil at riverbankcomputing.com
Tue Dec 16 08:32:59 GMT 2014


On 16/12/2014 6:47 am, Vincent Vande Vyvre wrote:
> Hi,
> 
> The 'connectSlotsByName' mechanism seems not working if the ui class
> don't inherit of the QMainWindow.
> This is the case when the .py file is created with pyuic.
> 
> The first example, below, don't works, the second example works

Both behave as the documentation says it should. In the first 
on_pushButton_clicked() is not a slot of MainWindow.

Phil

> Tested on Ubuntu 14.04
> Python: 3.4.0 (default, Apr 11 2014, 13:05:11) [GCC 4.8.2]
> sip:    4.15.5
> Qt:     5.2.1
> PyQt:   5.2.1
> 
> slottest.py
> -----%<------------------------------------------------------------
> # -*- coding: utf-8 -*-
> 
> # Created: Tue Dec 16 07:27:25 2014
> #      by: PyQt5 UI code generator 5.2.1
> 
> from PyQt5 import QtCore, QtGui, QtWidgets
> 
> class Ui_MainWindow(object):
>     def setupUi(self, MainWindow):
>         MainWindow.setObjectName("MainWindow")
>         MainWindow.resize(136, 95)
>         self.centralwidget = QtWidgets.QWidget(MainWindow)
>         self.centralwidget.setObjectName("centralwidget")
>         self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
>         self.gridLayout.setObjectName("gridLayout")
>         self.pushButton = QtWidgets.QPushButton(self.centralwidget)
>         self.pushButton.setText("PushButton")
>         self.pushButton.setObjectName("pushButton")
>         self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
>         MainWindow.setCentralWidget(self.centralwidget)
> 
>         QtCore.QMetaObject.connectSlotsByName(MainWindow)
> 
>     @QtCore.pyqtSlot()
>     def on_pushButton_clicked(self):
>         print("Button clicked")
> 
> if __name__ == "__main__":
>     import sys
>     app = QtWidgets.QApplication(sys.argv)
>     MainWindow = QtWidgets.QMainWindow()
>     ui = Ui_MainWindow()
>     ui.setupUi(MainWindow)
>     MainWindow.show()
>     sys.exit(app.exec_())
> -------------------------------------------------------------------
> 
> slottest1.py
> -----%<------------------------------------------------------------
> # -*- coding: utf-8 -*-
> from PyQt5.QtCore import QMetaObject, pyqtSlot
> from PyQt5.QtWidgets import QApplication, QWidget, QGridLayout, 
> QPushButton
> 
> class Widget(QWidget):
>     def __init__(self):
>         super(Widget, self).__init__()
>         gl = QGridLayout(self)
>         self.pushButton = QPushButton("Click me", self)
>         gl.addWidget(self.pushButton, 0, 0, 1, 1)
>         self.pushButton.setObjectName("pushButton")
>         QMetaObject.connectSlotsByName(self)
> 
>     @pyqtSlot()
>     def on_pushButton_clicked(self):
>         print("Button clicked")
> 
> if __name__ == "__main__":
>     import sys
>     app = QApplication(sys.argv)
>     w = Widget()
>     w.show()
>     sys.exit(app.exec_())
> -------------------------------------------------------------------
> 
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt


More information about the PyQt mailing list