【问题标题】:Qt show constantly updating percentage in status barQt在状态栏中显示不断更新的百分比
【发布时间】:2015-05-25 13:13:20
【问题描述】:

我有一个程序可以加载文件,对数据进行一些计算,将数据保存在内存中并打印到文本文件中。

我想在状态栏中显示处理进度的百分比,我使用如下代码:

    percent=file.pos()*10000/file.size();
    if((percent!=progress)&&(percent%5==1)){
        progress=percent/100;
        qDebug() << progress;
        progresslabel->setText(QString("Loading File: %1 \%").arg(progress));

这是以下函数的一部分:

void MainWindow::CalcIntegral(){

    int percent,progress;
    progress=1;
    SingleLineData.resize(GRIDDIM*GRIDDIM);
    SingleLineData.fill(0);

    QString test;
    QString inputfile = QFileDialog::getOpenFileName(
                this,
                tr("Open Data File"),
                "/Users",
                "Data files (*.txt)"
                );

    if(inputfile == NULL)
        return;
        QFile file(inputfile);

  qDebug()<<"TEST";
        if(!file.open(QIODevice::ReadOnly)){
        }
    //    QTextStream in(&file);
    OpenFileData = new QTextStream(&file);

        double buffer;
        OpenFilePositions.resize(0);
        OpenFilePositions.push_back(-2);
        OpenFileData->readLine();
        progresslabel = new QLabel;
        ui->statusBar->addWidget(progresslabel);
        while(!OpenFileData->atEnd()){

            for(int i=0; i<512; i++){
                *OpenFileData >> buffer;
                SingleLineData[i]+=buffer;
            }

            percent=file.pos()*10000/file.size();
            if((percent!=progress)&&(percent%5==1)){
                progress=percent/100;
                qDebug() << progress;
                progresslabel->setText(QString("Loading File: %1 \%").arg(progress));


            }

    OpenFilePositions.push_back(OpenFilePositions.last()+6*(NUMBER_OF_AFE_CHIPS*AFECHANNELS)+1);

  }
        OpenFilePositions.removeFirst();
        OpenFilePositions.push_front(0);

  qDebug() << SingleLineData;
  qDebug() << OpenFilePositions.size();
  // ************* file output **************************************************

//  QString filename = QFileDialog::getSaveFileName(
//              this,
//              tr("Save File"),
//              "/Users",
//              "Text files (*.txt)"
//              );


  QString OutputFile = inputfile;
  OutputFile.replace(".txt","_integral.int");
  QFile fileout(OutputFile);
  fileout.open(QFile::WriteOnly);
  QTextStream out(&fileout);
  for(int i=0;i<512;i++){
      out<<SingleLineData[i]<<"\t";
  }

  fileout.close();
}

感谢qDebug(),我可以在控制台/应用程序输出中看到不断更新的百分比,但是状态栏仅在处理完成后才更新并直接跳到 99%。

有什么办法让它实时更新吗?

干杯

【问题讨论】:

    标签: c++ qt statusbar progress


    【解决方案1】:

    您正在循环中更新进度状态,这会阻塞主 GUI 线程,因此您的标签文本不会得到更新。这是因为应该进行一些事件循环迭代来更新和处理与 GUI 相关的事件。一种可能的解决方案是在 while 循环中使用QApplication::processEvents:

    ...
    progresslabel->setText(QString("Loading File: %1 \%").arg(progress));
    qApp->processEvents(QEventLoop::AllEvents);
    ...
    

    另一种解决方案是在新线程中执行处理,并使用信号报告进度状态。只需在工作线程中准备一个定期发出的信号并将其连接到 GUI 线程中的插槽并在进度百分比内更新标签。

    【讨论】:

    • @MitchellD 不过要小心。在大多数情况下,它被认为是一种 hack,而不是一种解决方案。
    • @thuga 可以,只是作为临时解决方案,直到我开始使用不同的线程
    猜你喜欢
    • 2012-03-30
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 2013-05-17
    • 2020-02-15
    相关资源
    最近更新 更多