【发布时间】:2019-02-26 10:43:24
【问题描述】:
我正在使用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。
【问题讨论】:
-
你是在逐行读取文件吗?
-
是的,我正在逐行读取文件