2

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)
gravetii
  • 9,273
  • 9
  • 56
  • 75
Orcl User
  • 293
  • 1
  • 5
  • 20

3 Answers3

2

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.

Community
  • 1
  • 1
gravetii
  • 9,273
  • 9
  • 56
  • 75
  • Thank you..but my class is a Widget and not a Window: self.statusbar = self.statusBar() AttributeError: 'Widget' object has no attribute 'statusBar' – Orcl User Feb 10 '14 at 10:14
  • 1
    Then, try using the `QStatusBar()` method as written in the latter part of my answer. – gravetii Feb 10 '14 at 10:15
  • It is not showing anything..Just the table is shown.Maybe i should give dimensions to statusBar or something? – Orcl User Feb 10 '14 at 10:19
  • I guess my new edit is also the same, but still not working :(! I editted my Question.. – Orcl User Feb 10 '14 at 10:25
  • 1
    You need to assign the `QVBoxLayout` to a member object. The edit in my answer points to that: `self.layout = QVBoxLayout(self)` – gravetii Feb 10 '14 at 10:27
  • Glad I could help. You could also consider accepting the best answer which resolved your issue. :-) – gravetii Feb 10 '14 at 10:33
2

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

self.statusBar = QStatusBar()
self.statusBar.showMessage("Some message")
...
qurban
  • 3,885
  • 24
  • 36
1

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.

Predelnik
  • 5,066
  • 2
  • 24
  • 36
  • 1
    @OrclUser But you haven't done what I said: you are still creating temporary layout objects and maybe there are also some other problems pointed in other answers I just wanted to say that your way of layout creation isn't right. – Predelnik Feb 10 '14 at 10:36
  • The OP is using python, not C++. – thuga Feb 10 '14 at 11:42
  • @thuga Oops, looks like I messed up then) – Predelnik Feb 10 '14 at 11:45