shadownrun
shadownrun

Reputation: 364

pyqt4 emit signal doesn't work

I want to emit a triggered signal on the actionTracar Qaction element, but it doesn't work or give any error.

I have made a little snippet of the code to trouble shoot it. But I still can't figure it out.

Trouble.py:

# -*- coding: utf-8 -*-
import sys
from PyQt4.QtCore import SIGNAL
from PyQt4.QtGui import QApplication, QMainWindow, QDoubleValidator
from Tela import Ui_MainWindow
class JanelaPolinomio (QMainWindow, Ui_MainWindow):

    def __init__(self):        
        super(JanelaPolinomio, self).__init__()       
        self.setupUi(self)
        self.actionTracar.emit(SIGNAL("triggered()"))

    def on_actionTracar_triggered(self):
        print "test"

if __name__ == '__main__':
    app = QApplication(sys.argv)    

    jan = JanelaPolinomio()   
    jan.show()        

    sys.exit(app.exec_())

Tela.py:

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(390, 244)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 390, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.toolBar = QtGui.QToolBar(MainWindow)
        self.toolBar.setObjectName(_fromUtf8("toolBar"))
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
        self.actionTracar = QtGui.QAction(MainWindow)
        self.actionTracar.setObjectName(_fromUtf8("actionTracar"))
        self.toolBar.addAction(self.actionTracar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar", None))
        self.actionTracar.setText(_translate("MainWindow", "tracar", None))
        self.actionTracar.setShortcut(_translate("MainWindow", "Ctrl+T", None))

Any help is appreciated.

Upvotes: 0

Views: 545

Answers (1)

mata
mata

Reputation: 69042

The problem is that the triggered signal of a QAction has a different signature then what you're using as it takes a bool argument, so the correct way to emit such a signal using old style signals would be:

self.actionTracar.emit(SIGNAL("triggered(bool)"), False)

It is recommended to use new style signals in PyQt. If you tried the same with new style signals you would get en error which indicates what's wrong instead of silently failing:

>>> jan.actionTracar.triggered.emit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: QAction.triggered[bool] signal has 1 argument(s) but 0 provided

PyQt5 will honor the default argument of checked=false, so the same will work there, but PyQt4 doesn't so you need to give the argument explicitly:

>>> jan.actionTracar.triggered.emit(False)

A different way of emitting the signal is to simply call the trigger() method on the action, it has the same effect.

Upvotes: 1

Related Questions