您的位置:首页 > 编程语言 > Qt开发

Qt学习之路_10(Qt中statusBar,MessageBox和Timer的简单处理)

2014-06-25 20:14 302 查看
QStatus的使用:
  众所周知,状态栏一般显示系统的状态信息,比如进度,鼠标所在的行列等信息。这次是个简单的实验,在状态栏中加入进度条和label,以及用状态栏自带的方法显示信息,显示信息持续的时间可以由参数来确定。
注意状态栏下的addWidget和addPermenentWidge方法不同,addPermenentWidget是永久固定显示的,里面的内容不会更改,也不会被覆盖,而addWidget加入的widget在必要时候会被更改和覆盖。
  让状态栏显示文本不是采用setText()方法,而是采用showMessage().其第二个参数为显示该内容持续的时间,以毫秒为单位。

  实验的效果如下(不是永久载入):
  单击工具栏的hit me按钮后:
  


  没触发工具栏或者触发1s过后显示如下:
  


实验代码和注释如下:




#include "mainwindow.h"

#include "ui_mainwindow.h"

#include <QtGui>

#include <QtCore>

MainWindow::MainWindow(QWidget *parent) :

QMainWindow(parent),

ui(new Ui::MainWindow)

{

ui->setupUi(this);

//注意构造此函数时需要加入父窗口指针这一参数。

my_label = new QLabel(this);

my_progressbar = new QProgressBar(this);

ui->statusBar->addWidget(my_label);

ui->statusBar->addWidget(my_progressbar, 1);

//如果是永久固定,则是不能覆盖的,不能更改了,不像上面的可以被覆盖

//    ui->statusBar->addPermanentWidget(my_label);

//    //此处的参数1表示计算出的合适尺寸

//    ui->statusBar->addPermanentWidget(my_progressbar, 1);

my_label->setText("no reason");

my_progressbar->setTextVisible( true );

my_progressbar->setValue(30);

}

MainWindow::~MainWindow()

{

delete ui;

}

void MainWindow::on_actionHit_triggered()

{

//第二个参数为显示该内容持续的时间,以毫秒为单位。

ui->statusBar->showMessage("why do you hit me?", 1000);

}





 QMessageBox的使用:
QMessageBox在系统中常用的有下面几种风格,warning,question,information,其表现形式各有不同,仅从名字可以看出它给用户传递的消息本身就不同。
首先来简单的看看这几种消息box的特点。
标准的QMessageBox::information是一个OK按钮,当然这可以对它进行更改,可以加入其它扭。information的特点是有个!号。并且弹出该消息框后系统会发出响声
question的messagebox是带有个?号,弹出消息框后系统不发出响声。最后一个参数一定要用或,否则其返回结果是不能赋值给一个标准button replay的。
warning类型有个黄色的感叹号,也是发出一声警告声,没有information发出的的声音清脆,毕竟这是警告声。
  本次实验就是写程序试一下其各种类型的效果。

效果如下所示:
主界面:
  


Information类型:
  


Question类型:
  


  
Warning类型:
  


Custom类型:
  


本次实验的代码及注释如下:




#include "dialog.h"
#include "ui_dialog.h"
#include <QtCore>
#include <QtGui>

Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}

Dialog::~Dialog()
{
delete ui;
}

void Dialog::on_infButton_clicked()
{
//标准的QMessageBox::information是一个OK按钮,当然这可以更改,加入其它按钮
//information的特点是有个!号。并且弹出该消息框后系统会发出响声
QMessageBox::information(this, "Information", "the style of information",
QMessageBox::Yes, QMessageBox::No);
}

void Dialog::on_queButton_clicked()
{
QMessageBox::StandardButton replay;
//question的messagebox是有个?号,弹出消息框后系统不好发出响声
//最后一个参数一定要用或,否则是不能赋值给一个标准button replay的
replay = QMessageBox::question(this, "Question", "the style of question",
QMessageBox::Yes | QMessageBox::No);
if( replay == QMessageBox::Yes)
{
QMessageBox::information(this, "question", "Yes");
}
//如果直接用question类型的话,其对话框右上角是没有X号的,因为系统默认是要你选中的
//    QMessageBox::question(this, "Question", "the style of question",
//                                  QMessageBox::Yes | QMessageBox::No);
}

void Dialog::on_warButton_clicked()
{
//有个黄色的感叹号
//也有一声警告声,没有information的声音清脆,因为这是警告声
QMessageBox::warning(this, "Warning", "the stytle of warning");
}

void Dialog::on_cushButton_clicked()
{
//这里的消息类型可以自己随便定义
QMessageBox::question(this, "custom", "the stytle of custom",
QMessageBox::Yes | QMessageBox::YesToAll |
QMessageBox::No | QMessageBox::NoToAll);
}





  QTimer的使用:
  QTimer主要是用来计算时间的,可以计算启动时间什么的。这里主要是初始化一下QTimer,并设定其定时时间,定时时间一到,则发出timeout()信号,而定时器又重新开始计时。其发射出的信号传到到槽函数后,可以在槽函数中来完成自己的功能。

  运行效果如下:
  


实验代码如下:




#include "mytime.h"
#include <QtCore>
#include <QDebug>

MyTime::MyTime()
{
my_timer = new QTimer(this);
//timeout这个信号表示的是timer设定的时间已经到达时发出的。
connect(my_timer, SIGNAL(timeout()), this, SLOT(my_time()));
my_timer->start(2000);
}

void MyTime:: my_time()
{
qDebug () << "Timer is trigged!";
}





  总结:好像没什么可总结的,见代码和注释。

作者:tornadomeet 出处:http://www.cnblogs.com/tornadomeet 欢迎转载或分享,但请务必声明文章出处。 (新浪微博:tornadomeet,欢迎交流!)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: