首页 > 解决方案 > 在 QProgressBar 中显示进度

问题描述

我正在使用 QProgress 来显示读取配置文件的状态。我有一个类向主窗口发出信号以显示状态。目前,我可以显示状态,但它立即显示 100%,我希望它更可靠。就像我可以看到它从 0 到 5 到 20 到 45 等等,直到它达到 100%。这是我到目前为止吃过的东西:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new 
Ui::MainWindow)
{
    connect(sampleClass, SIGNAL(onDisplayStatus(int)), this, 
    SLOT(onDisplayStatus(int)));

    // OtherClass instance
    // config file has lots of fields to be read
    otherClass->validateConfigfile();
}

MainWindow::onDisplayStatus(int status)
{
    ui->ProgressStatus.setValue(status);
}

SampleClass::displayStatus(int value)
{
    emit onDisplayStatus(value);
}

// This will validate if the config file is valid or existing; the 
// progress will be set to 10 if valid and existing
OtherClass::validateConfigfile() 
{
    // instance of SampleClass
    //10 is the value of progress. 
    //I dont want it to be fixed. Pleas suggest how to properly compute 
    //the value
    sampleClassInstance->displayStatus(10); 

    loadSection1();
    loadSection2();
}

OtherClass::loadSection1()
{
    // load section 1 here
    sampleClassInstance->displayStatus(20);
}

OtherClass::loadSection2()
{
    // load section 2 here
    sampleClassInstance->displayStatus(35);
}

注意:我的配置文件包含许多字段。下面是示例:

[Section 1]
S1Field1 = 0
S1Field2 = 1
S1Field3 = 2

[Section 2]
S2Field1 = 0
S2Field2 = 1

[Section 3]
S3Field1 = 0
S3Field2 = 1
S3Field3 = 2
S3Field4 = 6
S3Field5 = 4
S3Field6 = 9

等等...

我在 OtherClass 中创建了一个方法,它将读取每个部分和字段。读取每个部分后,将显示进度值,直到进度达到 100。

标签: c++qt

解决方案


请查看文档:https ://doc.qt.io/qt-5/qprogressbar.html

您必须设置进度条的最大值和最小值。


推荐阅读