现在的位置: 首页 > 综合 > 正文

(转)3.3设置状态栏(Setting Up the Status Bar)

2014年02月08日 ⁄ 综合 ⁄ 共 1415字 ⁄ 字号 评论关闭

Spreadsheet应用程序的状态栏通常有两个指示符:一是当前单元位置,二是当前单元规则。状态栏也用于显示状态提示及其它临时消息。

void MainWindow::createStatusBar()
{
    locationLabel = new QLabel(" W999 ");
    locationLabel->setAlignment(Qt::AlignHCenter);
    locationLabel->setMinimumSize(locationLabel->sizeHint());
    formulaLabel = new QLabel;
    formulaLabel->setIndent(3);
    statusBar()->addWidget(locationLabel);
    statusBar()->addWidget(formulaLabel, 1);
    connect(spreadsheet, SIGNAL(currentCellChanged(int, int, int, int)),
            this, SLOT(updateStatusBar()));
    connect(spreadsheet, SIGNAL(modified()),
            this, SLOT(spreadsheetModified()));
    updateStatusBar();
}

QMainWindow::statusBar() 函数返回指向一个状态栏的指针(在第一次调用statusBar() 函数时就创建了状态栏)。状态栏的指示符是QLabel,必要时可对它的文本作改变。为formulaLabel添加缩进(Indent),可以使其显示的文本能从左边起有缩进。当QLabel被加到状态栏时,它们就自动成为了状态栏的孩子。

通过formulaLabel的QStatusBar::addWidget()调用中,指定伸展因子为1,可以实现当窗口大小变化时,伸展的空间仅在右边的单元规则指示符栏。而 locationLabel 的默认伸展因子为0,表明保持不变,不伸展。 

QStatusBar排列指示符控件时,是根据由QWidget::sizeHint() 获得的每个控件的理想大小进行布局的,并且伸展可被伸展的控件来填充可用空间。而控件的理想大小是依赖控件的内容并根据内容的变化而改变,为避免不断的变化,我们可以设置最小大小来确保最大文本("W999")的足够宽度。

最后,将槽updateStatusBar() 作为普通函数调用来初始化指示符,因为Spreadsheet在启动时并没发出currentCellChanged() 信号。 

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

void MainWindow::spreadsheetModified()
{
    setWindowModified(true);
    updateStatusBar();
}

spreadsheetModified() 槽设置windowModified属性为真,更新标题栏,同时也更新状态栏。

抱歉!评论已关闭.