PyQt - QStatusBar Widget



QMainWindow object reserves a horizontal bar at the bottom as the status bar. It is used to display either permanent or contextual status information.

There are three types of status indicators −

  • Temporary − Briefly occupies most of the status bar. For example, used to explain tool tip texts or menu entries.

  • Normal − Occupies part of the status bar and may be hidden by temporary messages. For example, used to display the page and line number in a word processor.

  • Permanent − It is never hidden. Used for important mode indications. For example, some applications put a Caps Lock indicator in the status bar.

Status bar of QMainWindow is retrieved by statusBar() function. setStatusBar() function activates it.

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

Methods of QStatusBar Class

Sr.No. Methods & Description
1

addWidget()

Adds the given widget object in the status bar

2

addPermanentWidget()

Adds the given widget object in the status bar permanently

3

showMessage()

Displays a temporary message in the status bar for a specified time interval

4

clearMessage()

Removes any temporary message being shown

5

removeWidget()

Removes specified widget from the status bar

Example

In the following example, a top level QMainWindow has a menu bar and a QTextEdit object as its central widget.

Window’s status bar is activated as explained above.

Menu’s triggered signal is passed to processtrigger() slot function. If ‘show’ action is triggered, it displays a temporary message in the status bar as −

if (q.text() == "show"):
   self.statusBar.showMessage(q.text()+" is clicked",2000)

The message will be erased after 2000 milliseconds (2 sec). If ‘add’ action is triggered, a button widget is added.

if q.text() == "add":
   self.statusBar.addWidget(self.b)

Remove action will remove the button from the status bar.

if q.text() == "remove":
   self.statusBar.removeWidget(self.b)
   self.statusBar.show()

The complete code is as follows −

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

class statusdemo(QMainWindow):
   def __init__(self, parent = None):
      super(statusdemo, self).__init__(parent)
		
      bar = self.menuBar()
      file = bar.addMenu("File")
      file.addAction("show")
      file.addAction("add")
      file.addAction("remove")
      file.triggered[QAction].connect(self.processtrigger)
      self.setCentralWidget(QTextEdit())
		
      self.statusBar = QStatusBar()
      self.b = QPushButton("click here")
      self.setWindowTitle("QStatusBar Example")
      self.setStatusBar(self.statusBar)
		
   def processtrigger(self,q):
	
      if (q.text() == "show"):
         self.statusBar.showMessage(q.text()+" is clicked",2000)
			
      if q.text() == "add":
         self.statusBar.addWidget(self.b)
			
      if q.text() == "remove":
         self.statusBar.removeWidget(self.b)
         self.statusBar.show()
			
def main():
   app = QApplication(sys.argv)
   ex = statusdemo()
   ex.show()
   sys.exit(app.exec_())
	
if __name__ == '__main__':
   main()

The above code produces the following output −

QStatusBar Widget Output
Advertisements