Phorce
Phorce

Reputation: 4642

Python Qt updating status bar during program execution

I'm reading the following tutorial:

Events and Signals in PyQt4

And the example shows the status bar being updated when a button is clicked, however, I'm wondering whether or not it is possible to do this as a generic function (say).. So for example, I could create a function that passes through a string and whenever I call this function, the statusBar changes to the text parsed through. I'm assuming you have to use connect but I've tried the following:

#in main have:
self.connect(showMessage)

def updateStatusBar(): 
    sender = self.sender()
    self.statusBar().showMessage(sender.text() + 'Here');

However this does not work. Could anyone please show me an alternative to doing this?

Upvotes: 0

Views: 5910

Answers (1)

Henry
Henry

Reputation: 141

Try to get hold of the signal emitted by the object you want to trigger the action. Here's a minimal example (updating the status bar whenever the text in the QLineEdit changes). I hope it does what you need.

import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MainWindow(QMainWindow):

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

        self.statusBar = QStatusBar()
        self.setStatusBar(self.statusBar)
        self.statusBar.showMessage('...')

        self.lineEdit = QLineEdit()
        self.setCentralWidget(self.lineEdit)

        self.lineEdit.textEdited.connect(self.updateStatusBar)

    def updateStatusBar(self, string): 
        self.statusBar.showMessage(string)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

See the docs for info about which signals are predefined for each class, e.g. http://qt-project.org/doc/qt-5.0/qtwidgets/qlineedit.html#signals.

Upvotes: 2

Related Questions