PyQt4 signals and slots

2024/10/18 15:32:29

I am writing my first Python app with PyQt4. I have a MainWindow and a Dialog class, which is a part of MainWindow class:

self.loginDialog = LoginDialog();

I use slots and signals. Here's a connection made in MainWindow:

QtCore.QObject.connect(self.loginDialog, QtCore.SIGNAL("aa(str)"), self.login)

And I try to emit signal inside the Dialog class (I'm sure it is emitted):

self.emit(QtCore.SIGNAL("aa"), "jacek")

Unfortunately, slot is not invoked. I tried with no arguments as well, different styles of emitting signal. No errors, no warnings in the code. What might be the problem?

Answer

There are some concepts to be clarified

[QT signal & slot] VS [Python signal & slot]

All the predefined signals & slots provided by pyqt are implemented by QT's c++ code. Whenever you want to have a customized signal & slot in Python, it is a python signal & slot. Hence there are four cases to emits a signal to a slot:

  • from a QT signal to a QT slot
  • from a QT signal to a Python slot
  • from a Python signal to a QT slot
  • from a Python signal to a Python slot

The code below shows how to connect for these four different scnarios

    import sysfrom PyQt4.QtCore import *from PyQt4.QtGui import *class Foo(QtCore.QObject):def __init__(self, parent=None):super(Foo, self).__init__(parent)dial = QDial()self.spinbox = QSpinbox()# --------------------------------------# QT signal & QT slot# --------------------------------------# option 1: more efficient self.connect(self.spinbox, SIGNAL("valueChanged(int)"), dial, SLOT("setValue(int)"))# option 2:self.connect(self.spinbox, SIGNAL("valueChanged(int)"), dial.setValue)# --------------------------------------# QT signal & Python slot# --------------------------------------self.connect(self.spinbox, SIGNAL("valueChanged(int)"), self.myValChanged)# --------------------------------------# Python signal & Qt slot# --------------------------------------# connect option 1: more efficientself.connect(self, SIGNAL("mysignal"), dial, SLOT("setValue(int)"))# connect option 2:self.connect(self, SIGNAL("mysignal"), dial.setValue)# emitparam = 100self.emit(SIGNAL("mysignal"), param)# --------------------------------------# Python signal & Python slot# --------------------------------------# connectself.connect(self, SIGNAL("mysignal"), self.myValChanged)# emitparam = 100self.emit(SIGNAL("mysignal"), param)def myValChanged(self):print "New spin val entered {0}".format(self.spinbox.value())

Conclusion is --

Signal signature for Python signal differentiate from that of QT signal in that it doesn't have the parenthesis and can be passed any python data types when you emit it. The Python signal is created when you emit it.

For slot, there are three forms of signatures.

  • s.connect(w, SIGNAL("signalSignature"), functionName)
  • s.connect(w,SIGNAL("signalSignature"), instance.methodName)
  • s.connect(w,SIGNAL("signalSignature"), instance, SLOT("slotSignature"))

Number 1 & 2 are available for Python slot, while number 2 & 3 are available for QT slot. It is clear that besides QT predefined slot, any python callable function/methods is qulified to be a Python slot.

These points are made in Summerfield's article on Signals and Slots.

[Old style qt signal & slot] VS [new style qt singal & slot]

Well, all the description above is based on the old style pyqt signal & slot. As @Idan K suggested there is an alternative new-style to do the things, especially for the Python signal. Refer to here for more.

https://en.xdnf.cn/q/72831.html

Related Q&A

Masking a pandas DataFrame with a numpy array vs DataFrame

I want to use a 2D boolean mask to selectively alter some cells in a pandas DataFrame. I noticed that I cannot use a numpy array (successfully) as the mask, but I can use a DataFrame. More frustratin…

How to explode multiple columns, different types and different lengths?

Ive got a DF with columns of different time cycles (1/6, 3/6, 6/6 etc.) and would like to "explode" all the columns to create a new DF in which each row is a 1/6 cycle.from pyspark import Row…

How to convert \xXY encoded characters to UTF-8 in Python?

I have a text which contains characters such as "\xaf", "\xbe", which, as I understand it from this question, are ASCII encoded characters. I want to convert them in Python to their…

Pandas One hot encoding: Bundling together less frequent categories

Im doing one hot encoding over a categorical column which has some 18 different kind of values. I want to create new columns for only those values, which appear more than some threshold (lets say 1%), …

How to pass classs self through a flask.Blueprint.route decorator?

I am writing my websites backend using Flask and Python 2.7, and have run into a bit of a problem. I like to use classes to enclose my functions, it makes things neat for me and helps me keep everythin…

why cannot I use sp.signal by import scipy as sp? [duplicate]

This question already has an answer here:scipy.special import issue(1 answer)Closed 8 years ago.I would like to use scipy.signal.lti and scipy.signal.impulse function to calculate the transfer function…

How to speed up nested cross validation in python?

From what Ive found there is 1 other question like this (Speed-up nested cross-validation) however installing MPI does not work for me after trying several fixes also suggested on this site and microso…

Streaming video from camera in FastAPI results in frozen image after first frame

I am trying to stream video from a camera using FastAPI, similar to an example I found for Flask. In Flask, the example works correctly, and the video is streamed without any issues. However, when I tr…

Fastest way to concatenate multiple files column wise - Python

What is the fastest method to concatenate multiple files column wise (within Python)?Assume that I have two files with 1,000,000,000 lines and ~200 UTF8 characters per line.Method 1: Cheating with pas…

Can autograd in pytorch handle a repeated use of a layer within the same module?

I have a layer layer in an nn.Module and use it two or more times during a single forward step. The output of this layer is later inputted to the same layer. Can pytorchs autograd compute the grad of t…