c++ - 在 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。
解决方案
请查看文档:https ://doc.qt.io/qt-5/qprogressbar.html
您必须设置进度条的最大值和最小值。
推荐阅读
- reactjs - react codemirror 2如何显示之前输入的变量
- c# - 获取从今天开始 1 个月的日期,UTC,以秒为单位。C#
- vue.js - Eslinter 不适用于具有 .vue 扩展名的文件
- xcode - 在 watch app swift 中应该使用哪种设计模式?
- python-3.x - Python selenium 无法正确找到类
- html - 当用户在数字输入中超过最大值时如何禁用浏览器弹出/工具提示?
- javascript - 将 .each jQuery 更改为 toArray()
- node.js - 在 node js 和 react-native app 之间建立连接
- azure - .NET Core Azure AD 应用注册——允许通过组访问
- sql - 如何根据字段日期获取大查询的最新记录