pythonqtpyqtqstatusbar

Add QStatusBar in a QVBoxLayout


I need to add a status bar beneath a table in a QVBoxLayout. The problem is that I don't know why status bar is not shown. In the QBoxLayout, I have added a tableView, under the table I need to have the status bar. Here is part of my code:

  self.setGeometry(200,200,600,600)
  if self._model.productName()!='':
    self.setWindowTitle('TITLE')
  QVBoxLayout(self).addWidget(self.tv)

  # add staus bar
  statusBar = QStatusBar()
  statusLabel = QLabel("Here comes the status bar message!!")
  statusBar.addWidget(statusLabel)
  QVBoxLayout(self).addWidget(statusBar)

Solution

  • The ideal way to show a status bar would be to first inherit from the QtGui.QMainWindow class and then use the statusBar method to create the status bar.

    so inside your main class that creates the GUI, you can do this:

    class Window(QtGui.QMainWindow):
        def __init__(self, parent):
           super(Window, self).__init__()
           self.statusbar = self.statusBar()
    

    You can then show a message in the status bar in the following manner:

    self.statusbar.showMessage('This message will be shown in the status bar')
    

    It is not required to use QLabel to show a status bar message.

    Alternatively, you can inherit from the QtGui.QWidget class and do this:

    self.statusbar = QStatusBar()
    self.statusbar.showMessage('Some status bar message')
    

    Also, as one of the other answers points out, you are wrong with how the layout is created.

    self.layout = QVBoxLayout(self)
    self.layout.addWidget(self.tv)
    

    This should be the correct way to do it.