The status bar

The status bar is the space at the bottom of the main window that is used to provide some additional information to the user.

It quite easy to use it in Qt, it is available in any QMainWindow, and it is showed automatically since the first time we gain access to it, calling the member function statusBar().

We have seen in a previous post how to let an action to put a message on the status bar, this is the so called "status tip" access to the status bar, and here is how we tell to an action what we what to be showed to the user when it is ready to be called:

QAction* actNew = new QAction(tr("&New"), this);
// ...
actNew->setStatusTip(tr("Set new message text"));
// ...


The normal access to the status bar is slightly complex. First thing we have to specify where we want to put the message. This means that usually we create a label and say to the status bar that that label has to be showed in its area.

Here we want just a simple status bar message, so we change the definition of our main window class definition adding a label and a couple of private functions to create and update the status bar, in this way:

#include
#include

class QCloseEvent;
class QAction;

class WinMain : public QMainWindow
{
Q_OBJECT
public:
explicit WinMain(QWidget* parent = 0);

private:
// ...
void createStatusBar();
void updateStatusBar(QString message);

QLabel lblMessage; // status bar message

// ...
};

What we want to show normally in the status bar, is just the same message we show to the user in the central widget, so I just slightly changed the function called by the main window constructor to initialize and update the status bar - in the meantime I have also changed the name of the function itself, giving it a more meaningful name:

void WinMain::setCentralWidget()
{
QString hello("Hello Qt!");
QLabel* pLb = new QLabel(this);
pLb->setText(hello);

QFont f("Times", 15, 0, true);
pLb->setFont(f);
pLb->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

QMainWindow::setCentralWidget(pLb); // 1.

createStatusBar();
updateStatusBar(hello);
}

1. since I gave name "setCentralWidget()" to this function, to call the QMainWindow setCentralWindow() I now have to specify the fully qualified named.

The createStatusBar() function has actually a misleading name, since it does not properly create the status bar, just add the label to the status bar itself:

void WinMain::createStatusBar()
{
statusBar()->addWidget(&lblMessage, 1);
}

The "1" passed as second parameter to the addWidget() has the meaning of reserving all the room on the status bar to this widget, and not just the minimal required space.

Updating the status bar is just a matter of updating the label whose address we passed to it:

void WinMain::updateStatusBar(QString message)
{
lblMessage.setText(message);
}

There is a third way of putting a message on the status bar: showing a temporary message.

Let's say that we want to show a temporary message when the dialog used to change the text in the central widget is active. We have a couple of choices: does we want to show the message for a short period, let say a second, or do we want to keep the message on for all the time the dialog is there?
We can do both way using the same function, just passing or not an optional parameter that specify the time, in millisecs, we want to keep the message visible. But remember, if you don't specify the time, you should remember to explicitly turn off the message.

I rewrote the newMessage function adding this functionality:

void WinMain::newMessage()
{
// statusBar()->showMessage("Gimme a new message");
statusBar()->showMessage("Gimme a new message", 1000);

DlgMessage dlgMessage(this);
// ...

// statusBar()->clearMessage();
}

I should use the couple of function showMessage() - clearMessage(), or just the showMessage() with the time specified as second parameter.

No comments:

Post a Comment