Setting Up the Status Bar

With the menus and toolbars complete, we are ready to tackle the Spreadsheet application's status bar. In its normal state, the status bar contains three indicators: the current cell's location, the current cell's formula, and MOD. The status bar is also used to display status tips and other temporary messages.

The MainWindow constructor calls createStatusBar() to set up the status bar:

void MainWindow::createStatusBar()
{
 locationLabel = new QLabel(" W999 ", this);
 locationLabel->setAlignment(AlignHCenter);
 locationLabel->setMinimumSize(locationLabel->sizeHint());

 formulaLabel = new QLabel(this);

 modLabel = new QLabel(tr(" MOD "), this);
 modLabel->setAlignment(AlignHCenter);
 modLabel->setMinimumSize(modLabel->sizeHint());
 modLabel->clear();

 statusBar()->addWidget(locationLabel);
 statusBar()->addWidget(formulaLabel, 1);
 statusBar()->addWidget(modLabel);

 connect(spreadsheet, SIGNAL(currentChanged(int, int)),
 this, SLOT(updateCellIndicators()));
 connect(spreadsheet, SIGNAL(modified()),
 this, SLOT(spreadsheetModified()));

 updateCellIndicators();
}

The QMainWindow::statusBar() function returns a pointer to the status bar. (The status bar is created the first time statusBar() is called.) The status indicators are simply QLabels whose text we change whenever necessary. When constructing the QLabels, we pass this as the parent, but it doesn't really matter since QStatusBar::addWidget() automatically "reparents" them to make them children of the status bar.

Figure 3.13 shows that the three labels have different space requirements. The cell location and MOD indicators require very little space, and when the window is resized, any extra space should go to the cell formula indicator in the middle. This is achieved by specifying a stretch factor of 1 in its QStatusBar::addWidget() call. The other two indicators have the default stretch factor of 0, meaning that they prefer not to be stretched.

Figure 3.13. The Spreadsheet application's status bar

graphics/03fig13.gif

When QStatusBar lays out indicator widgets, it tries to respect each widget's ideal size as given by QWidget::sizeHint() and then stretches any stretchable widgets to fill the available space. A widget's ideal size is itself dependent on the widget's content and varies as we change the content. To avoid constant resizing of the location and MOD indicators, we set their minimum sizes to be wide enough to contain the largest possible text on each of the indicators ("W999" and "MOD"), with a little extra space. We also set their alignment to AlignHCenter to horizontally center their text.

Near the end of the function, we connect two of Spreadsheet's signals to two of MainWindow's slots: updateCellIndicators() and spreadsheetModified().

void MainWindow::updateCellIndicators()
{
 locationLabel->setText(spreadsheet->currentLocation());
 formulaLabel->setText(" " + spreadsheet->currentFormula());
}

The updateCellIndicator() slot updates the cell location and the cell formula indicators. It is called whenever the user moves the cell cursor to a new cell. The slot is also used as an ordinary function at the end of createStatusBar() to initialize the indicators. This is necessary because Spreadsheet doesn't emit a currentChanged() signal at startup.

void MainWindow::spreadsheetModified()
{
 modLabel->setText(tr("MOD"));
 modified = true;
 updateCellIndicators();
}

The spreadsheetModified() slot updates all three indicators so that they reflect the current state of affairs, and sets the modified variable to true. (We used the modified variable when implementing the File menu to determine whether or not there were unsaved changes.)

Part I: Basic Qt

Getting Started

Creating Dialogs

Creating Main Windows

Implementing Application Functionality

Creating Custom Widgets

Part II: Intermediate Qt

Layout Management

Event Processing

2D and 3D Graphics

Drag and Drop

Input/Output

Container Classes

Databases

Networking

XML

Internationalization

Providing Online Help

Multithreading

Platform-Specific Features



C++ GUI Programming with Qt 3
C++ GUI Programming with Qt 3
ISBN: 0131240722
EAN: 2147483647
Year: 2006
Pages: 140

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net