Orcl User
Orcl User

Reputation: 293

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)

Upvotes: 2

Views: 2542

Answers (3)

Predelnik
Predelnik

Reputation: 5246

Well you're being wrong on managing layouts, for layout to be implemented it needs to live as long as the widget where you are using it lives. So basically that you have to do is to create layout with new and place it inside some widget.

What you are doing currently is creating layout and using it for self with QVBoxLayout(self) which is temporary variable and then creating another one which is once again temporary.

Correct way to create layout would be:

 QVBoxLayout *layout = new QVBoxLayout (self);
 layout->addWidget (...);
 layout->addWidget (...);

EDIT: This answer was written for C++ though it still points to valid mistake in author's code, so I won't delete it as it may still be useful to somebody.

Upvotes: 1

gravetii
gravetii

Reputation: 9654

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.

Upvotes: 2

qurban
qurban

Reputation: 3945

You don't need to add the QLabel in QStatusBar, just do the following:

self.statusBar = QStatusBar()
self.statusBar.showMessage("Some message")
...

Upvotes: 2

Related Questions