2011-12-15 63 views
6

嗯,我有一個QProgressBar其中i顯示下載進度,但是我想設置它顯示比例的下載速度,把它當作:更改文本

百分比%(downloadspeed KB/S)

有什麼想法?

回答

18

使QProgressBar文本可見。

QProgressBar *progBar = new QProgressBar(); 
progBar->setTextVisible(true); 

顯示下載進度

void Widget::setProgress(int downloadedSize, int totalSize) 
{ 
    double downloaded_Size = (double)downloadedSize; 
    double total_Size = (double)totalSize; 
    double progress = (downloaded_Size/total_Size) * 100; 
    progBar->setValue(progress); 

    // ****************************************************************** 
    progBar->setFormat("Your text here. "+QString::number(progress)+"%"); 
} 
+0

我想我解釋自己錯了(如果是這樣,對不起。)我想添加更多的文字到酒吧。因爲它只顯示百分比。 – Kazuma 2011-12-15 04:27:29

+1

編輯......... – 2011-12-15 05:52:00

5

你可以計算自己的下載速度,然後創建一個字符串這樣的:

QString text = QString("%p% (%1 KB/s)").arg(speedInKbps); 
progressBar->setFormat(text); 

你需要每次都做你的然而,下載速度需要更新。

2

由於QProgressBar for Macintosh StyleSheet不支持format屬性,因此需要跨平臺支持才能製作,您可以使用QLabel添加第二個圖層。

// init progress text label 
    if (progressBar->isTextVisible()) 
    { 
     progressBar->setTextVisible(false); // prevent dublicate 

     QHBoxLayout *layout = new QHBoxLayout(progressBar); 
     QLabel *overlay = new QLabel(); 
     overlay->setAlignment(Qt::AlignCenter); 
     overlay->setText(""); 
     layout->addWidget(overlay); 
     layout->setContentsMargins(0,0,0,0); 

     connect(progressBar, SIGNAL(valueChanged(int)), this, SLOT(progressLabelUpdate())); 
    } 

void MainWindow::progressLabelUpdate() 
{ 
    if (QProgressBar* progressBar = qobject_cast<QProgressBar*>(sender())) 
    { 
     QString text = progressBar->format(); 
     int precent = 0; 
     if (progressBar->maximum()>0) 
      precent = 100 * progressBar->value()/progressBar->maximum(); 
     text.replace("%p", QString::number(precent)); 
     text.replace("%v", QString::number(progressBar->value())); 
     QLabel *label = progressBar->findChild<QLabel *>(); 
     if (label) 
      label->setText(text); 
    } 
}