八、Qt Creator实现状态栏显示
2009-12-13 00:34
239 查看
在程序主窗口Mainwindow中,有菜单栏,工具栏,中心部件和状态栏。前面几个已经讲过了,这次讲解状态栏的使用。
程序中有哪些不明白的类或函数,请自己查看帮助。
1.我们在mainwindow.h中做一下更改。
加入头文件包含: #include <QLabel>
加入私有变量和函数:
QLabel* first_statusLabel; //声明两个标签对象,用于显示状态信息
QLabel* second_statusLabel;
void init_statusBar(); //初始化状态栏
加入一个槽函数声明:void do_cursorChanged(); //获取光标位置信息
2.在mainwindow.cpp中加入状态栏初始化函数的定义。
void MainWindow::init_statusBar()
{
QStatusBar* bar = ui->statusBar; //获取状态栏
first_statusLabel = new QLabel; //新建标签
first_statusLabel->setMinimumSize(150,20); //设置标签最小尺寸
first_statusLabel->setFrameShape(QFrame::WinPanel); //设置标签形状
first_statusLabel->setFrameShadow(QFrame::Sunken); //设置标签阴影
second_statusLabel = new QLabel;
second_statusLabel->setMinimumSize(150,20);
second_statusLabel->setFrameShape(QFrame::WinPanel);
second_statusLabel->setFrameShadow(QFrame::Sunken);
bar->addWidget(first_statusLabel);
bar->addWidget(second_statusLabel);
first_statusLabel->setText(tr("欢迎使用文本编辑器")); //初始化内容
second_statusLabel->setText(tr("yafeilinux制作!"));
}
这里将两个标签对象加入到了主窗口的状态栏里,并设置了他们的外观和初值。
3.在构造函数里调用状态栏初始化函数。
init_statusBar();
这时运行程序,效果如下。
![](http://hiphotos.baidu.com/yafeilinux/pic/item/8adc71f598f42840dcc4747c.jpg)
4.在mainwindow.cpp中加入获取光标位置的函数的定义。
void MainWindow::do_cursorChanged()
{
int rowNum = ui->textEdit->document()->blockCount();
//获取光标所在行的行号
const QTextCursor cursor = ui->textEdit->textCursor();
int colNum = cursor.columnNumber();
//获取光标所在列的列号
first_statusLabel->setText(tr("%1行 %2列").arg(rowNum).arg(colNum));
//在状态栏显示光标位置
}
这个函数可获取文本编辑框中光标的位置,并显示在状态栏中。
5.在构造函数添加光标位置改变信号的关联。
connect(ui->textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(do_cursorChanged()));
这时运行程序。效果如下。
![](http://hiphotos.baidu.com/yafeilinux/pic/item/c39730c3e143001be4dd3b7c.jpg)
6.在do_file_Load函数的最后添加下面语句。
second_statusLabel->setText(tr("打开文件成功"));
![](http://hiphotos.baidu.com/yafeilinux/pic/item/ebde94c51c64c6878326ac7d.jpg)
7.在saveFile函数的最后添加以下语句。
second_statusLabel->setText(tr("保存文件成功"));
![](http://hiphotos.baidu.com/yafeilinux/pic/item/c6e87809ef199a1ee824887d.jpg)
8.在on_action_Find_triggered函数的后面添加如下语句。
second_statusLabel->setText(tr("正在进行查找"));
![](http://hiphotos.baidu.com/yafeilinux/pic/item/f82b77fb8e1ca3b958ee907d.jpg)
9.在on_action_Close_triggered函数最后添加如下语句。
first_statusLabel->setText(tr("文本编辑器已关闭"));
second_statusLabel->setText(tr("yafeilinux制作!"));
![](http://hiphotos.baidu.com/yafeilinux/pic/item/0268db0eebda7ec3aa64577c.jpg)
最终的mainwindow.cpp文件内容如下。
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/36c16294c5c387267af480d4.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/96dce217c0432a7ff2de32d4.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/6b3dfd8af9a72dfdfc1f10d4.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/ee3a2d675baf8c0aaa184cd4.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/8593294fc0e15427b2de05d4.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/122a5172346d36328601b0d5.jpg)
最终的mainwindow.h文件如下。
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/6e14aa38bfbfbfdd3b87ced2.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/cf1ade45b68bdcacb3b7dcd2.jpg)
![](http://img.baidu.com/hi/jx/j_0043.gif)
到这里整个文本编辑器的程序就算写完了。我们这里没有写帮助菜单的功能实现,大家可以自己添加。而且程序中也有很多漏洞和不完善的地方,如果有兴趣,大家也可以自己修改。因为时间和篇幅的原因,我们这里就不再过多的讲述。
程序中有哪些不明白的类或函数,请自己查看帮助。
1.我们在mainwindow.h中做一下更改。
加入头文件包含: #include <QLabel>
加入私有变量和函数:
QLabel* first_statusLabel; //声明两个标签对象,用于显示状态信息
QLabel* second_statusLabel;
void init_statusBar(); //初始化状态栏
加入一个槽函数声明:void do_cursorChanged(); //获取光标位置信息
2.在mainwindow.cpp中加入状态栏初始化函数的定义。
void MainWindow::init_statusBar()
{
QStatusBar* bar = ui->statusBar; //获取状态栏
first_statusLabel = new QLabel; //新建标签
first_statusLabel->setMinimumSize(150,20); //设置标签最小尺寸
first_statusLabel->setFrameShape(QFrame::WinPanel); //设置标签形状
first_statusLabel->setFrameShadow(QFrame::Sunken); //设置标签阴影
second_statusLabel = new QLabel;
second_statusLabel->setMinimumSize(150,20);
second_statusLabel->setFrameShape(QFrame::WinPanel);
second_statusLabel->setFrameShadow(QFrame::Sunken);
bar->addWidget(first_statusLabel);
bar->addWidget(second_statusLabel);
first_statusLabel->setText(tr("欢迎使用文本编辑器")); //初始化内容
second_statusLabel->setText(tr("yafeilinux制作!"));
}
这里将两个标签对象加入到了主窗口的状态栏里,并设置了他们的外观和初值。
3.在构造函数里调用状态栏初始化函数。
init_statusBar();
这时运行程序,效果如下。
![](http://hiphotos.baidu.com/yafeilinux/pic/item/8adc71f598f42840dcc4747c.jpg)
4.在mainwindow.cpp中加入获取光标位置的函数的定义。
void MainWindow::do_cursorChanged()
{
int rowNum = ui->textEdit->document()->blockCount();
//获取光标所在行的行号
const QTextCursor cursor = ui->textEdit->textCursor();
int colNum = cursor.columnNumber();
//获取光标所在列的列号
first_statusLabel->setText(tr("%1行 %2列").arg(rowNum).arg(colNum));
//在状态栏显示光标位置
}
这个函数可获取文本编辑框中光标的位置,并显示在状态栏中。
5.在构造函数添加光标位置改变信号的关联。
connect(ui->textEdit,SIGNAL(cursorPositionChanged()),this,SLOT(do_cursorChanged()));
这时运行程序。效果如下。
![](http://hiphotos.baidu.com/yafeilinux/pic/item/c39730c3e143001be4dd3b7c.jpg)
6.在do_file_Load函数的最后添加下面语句。
second_statusLabel->setText(tr("打开文件成功"));
![](http://hiphotos.baidu.com/yafeilinux/pic/item/ebde94c51c64c6878326ac7d.jpg)
7.在saveFile函数的最后添加以下语句。
second_statusLabel->setText(tr("保存文件成功"));
![](http://hiphotos.baidu.com/yafeilinux/pic/item/c6e87809ef199a1ee824887d.jpg)
8.在on_action_Find_triggered函数的后面添加如下语句。
second_statusLabel->setText(tr("正在进行查找"));
![](http://hiphotos.baidu.com/yafeilinux/pic/item/f82b77fb8e1ca3b958ee907d.jpg)
9.在on_action_Close_triggered函数最后添加如下语句。
first_statusLabel->setText(tr("文本编辑器已关闭"));
second_statusLabel->setText(tr("yafeilinux制作!"));
![](http://hiphotos.baidu.com/yafeilinux/pic/item/0268db0eebda7ec3aa64577c.jpg)
最终的mainwindow.cpp文件内容如下。
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/36c16294c5c387267af480d4.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/96dce217c0432a7ff2de32d4.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/6b3dfd8af9a72dfdfc1f10d4.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/ee3a2d675baf8c0aaa184cd4.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/8593294fc0e15427b2de05d4.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/122a5172346d36328601b0d5.jpg)
最终的mainwindow.h文件如下。
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/6e14aa38bfbfbfdd3b87ced2.jpg)
![](http://hiphotos.baidu.com/yafeilinux/abpic/item/cf1ade45b68bdcacb3b7dcd2.jpg)
![](http://img.baidu.com/hi/jx/j_0043.gif)
到这里整个文本编辑器的程序就算写完了。我们这里没有写帮助菜单的功能实现,大家可以自己添加。而且程序中也有很多漏洞和不完善的地方,如果有兴趣,大家也可以自己修改。因为时间和篇幅的原因,我们这里就不再过多的讲述。
相关文章推荐
- 八、Qt Creator实现状态栏显示
- Qt Creator实现状态栏显示
- dede实现首页、栏目页、内容页的会员同步显示登陆状态
- 利用BCGControlBar 实现状态条显示Progressbar
- 使用QTextEdit实现状态信息显示框
- 脚本实现获取cloudstack中资源和一些状态信息以html报表显示
- Android中如何实现高亮显示即选中状态
- 脚本实现获取cloudstack中资源和一些状态信息以html报表显示
- Android4.1 如何实现状态栏上信号图标有SIM卡1,2标记,并且当处于2G状态显示“G”,处于3G状态显示“3G”
- Android中监听软键盘显示状态实现代码
- Winform开发框架中实现信息阅读状态的显示和存储
- Android编程实现控件不同状态文字显示不同颜色的方法
- JS实现点击复选框变更DIV显示状态的示例代码
- Android之界面切换并且图标显示状态效果实现
- MFC 位图按钮CBitmapButton 实现不同状态显示不同位图
- 网络请求加载控件的简单实现,能显示加载中、没网络、加载失败点击重新加载等状态
- js实现显示当前状态的导航效果代码
- 实现审核状态显示,选中行颜色变化功能。
- .NET-修改TreeView控件使实现CheckBoxs父子选中状态同步和自动显示调整
- 使用jQuery Uploadify借助Dorado平台实现批量上传文件动态显示实时上传文件状态