[PyQt] The 'connectSlotsByName' mechanism seems not working

Vincent Vande Vyvre vincent.vande.vyvre at telenet.be
Tue Dec 16 06:47:21 GMT 2014


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

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_())
-------------------------------------------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: slottest.py
Type: text/x-python
Size: 1195 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141216/da92581e/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: slottest1.py
Type: text/x-python
Size: 791 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141216/da92581e/attachment-0001.py>


More information about the PyQt mailing list