Qt中statusBar,MessageBox和Timer的簡單處理

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,歡迎交流!)
發佈了8 篇原創文章 · 獲贊 6 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章