3-3 创建状态条(Setting Up the Status Bar)

 
3-3 创建状态条(Setting Up the Status Bar)
完成菜单和工具条后,我们开始创建SpreadSheet应用程序的状态条。
在通常情况下,状态条提示两条信息:当前的格子的位置,和当前格子的公式。状态条还能够根据情况显示程序当前的运行状态和其他临时的信息。
在MainWindow的构造函数中,调用createStatusBar()创建状态条。代码如下:
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(
intintintint)),
            
this, SLOT(updateStatusBar()));
    connect(spreadsheet, SIGNAL(modified()),
            
this, SLOT(spreadsheetModified()));
    updateStatusBar();
}

QMainWindow::statusBar()函数返回状态条的指针(和QMainWindow::menuBar()一样,状态条在第一次调用这个函数的时候创建)。状态条的指示控件是QLable,根据程序的状态改变显示的信息。我们给formulaLabel设置了一个缩进,使它显示的信息离左边有一定的偏移。当QLabel控件加到状态条上时,它们的父对象就自动变成了状态条。
下图显示了状态条的两个文本框对控件的不同需求。格子的位置占用的控件较小,当窗口的大小改变时,其他多余的空间由formulaLabel占据,这是因为我们在代码中statusBar()->addWidget(formulaLabel, 1),设置了formularLabel的托放因数为1。而locationLabel的托放因数为默认的0,这表明它的大小是固定的。
QStatusBar放置指示控件时,它首先查看控件的QWidget::sizeHint()得到控件的合适大小。然后托放那些可托放的控件填到其他可用的空间。一个控件的理想大小是控件自己根据它显示的内容决定的,当显示内容改变时,这个大小也会改变。为了避免频繁改变locationLabel的大小,我们设置它的最小值为它可能显示的最大的文本(“W199 ”),对齐方式为水平居中对齐(Qt::AlignHCenter)。
在函数的最后,我们连接Spreadsheet控件的信号到MainWindow的槽函数:updataStatusBar()和spreadsheetModified()。
void  MainWindow::updateStatusBar()
{
    locationLabel
-> setText(spreadsheet -> currentLocation());
    formulaLabel
-> setText(spreadsheet -> currentFormula());
}
updateStatusBar()更新位置和公式的显示。只要用户移动图标到一个新的格子这个函数就会被调用。在createStatusBar()的最后它做为普通函数调用初始化状态条的显示,这样做是因为最开始Spreadsheet不发送currentCellChanged()信号。
 
void  MainWindow::spreadsheetModified()
{
    setWindowModified(
true );
    updateStatusBar();
}
spreadsheetModified()槽函数设置windowModified属性为true,用来更新窗口标题。然后调用updateStausBar()反映当前状态的变化。
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值