Vivek Dwivedi
Vivek Dwivedi

Reputation: 89

pyqt clicked.connect signaling more than one event

I have a button in a pyQt gui that signals an external class method using functools.partial:

self.Valve_ON.clicked.connect(functools.partial(self.ValveControl.IO_on,6008))

I would also like to be able to display a message in the statusBar

self.statusBar().showMessage("Valve on")

How can I signal more than one event on a clicked.

Thanks

Upvotes: 1

Views: 1198

Answers (2)

tacaswell
tacaswell

Reputation: 87556

You can connect as many slots to a signal as you want:

self.Valve_ON.clicked.connect(functools.partial(self.ValveControl.IO_on,6008))
self.Valve_ON.clicked.connect(functools.partial(self.statusBar().showMessage,"Valve on"))

Both should fire when you click the button.

A signal can be connected to an arbitrary number of slots and a slot can have an arbitrary number of signals connected to it. The library will sort out all of the dispatching for you.

Upvotes: 0

user1006989
user1006989

Reputation:

Create a slot for the signal and run your code from there, something like this:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

#---------
# IMPORT
#---------
import sys

from PyQt4 import QtGui, QtCore

#---------
# DEFINE
#---------
class MyWindow(QtGui.QMainWindow):
    _numberClicked = 0

    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        self.centralwidget = QtGui.QWidget(self)

        self.pushButtonClick = QtGui.QPushButton(self.centralwidget)
        self.pushButtonClick.setText("Click Me!")
        self.pushButtonClick.clicked.connect(self.on_pushButtonClick_clicked)

        self.labelClicked = QtGui.QLabel(self)

        self.layoutVertical = QtGui.QVBoxLayout(self.centralwidget)
        self.layoutVertical.addWidget(self.pushButtonClick)
        self.layoutVertical.addWidget(self.labelClicked)

        self.statusbar = QtGui.QStatusBar(self)
        self.statusbar.setObjectName("statusbar")

        self.setCentralWidget(self.centralwidget)
        self.setStatusBar(self.statusbar)

    @QtCore.pyqtSlot()
    def on_pushButtonClick_clicked(self):
        self._numberClicked += 1

        message = "Clicked {0} time(s)".format(self._numberClicked)

        self.labelClicked.setText(message)
        self.statusbar.showMessage(message, 1111)

#---------
# MAIN
#---------
if __name__ == "__main__":    
    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow()
    main.resize(333, 111)
    main.show()

    sys.exit(app.exec_())

Upvotes: 1

Related Questions