在qprogressbar中显示进度
Display progress in QProgressBar
我正在使用qprogress显示读取配置文件的状态。我有一个将信号发射到主窗口以显示状态的类。目前,我可以显示状态,但立即显示100%,我希望它更可靠。就像我看到的那样,例如从0到5到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
等等...
我在其他班级中创建了一个方法,该方法将读取每个部分和字段。一旦读取每个部分,直到进度达到100。
请查看文档:https://doc.qt.io/qt-5/qprogressbar.html
您必须设置进度键的最大和最小值。
如果您在主线程中处理(QT GUI的线程),则您的小部件将在完成之前不会更新/重新粉刷,然后直接显示100%。
为了正确使用ProgressBar,您需要在其他过程或某些异步代码中处理一些代码。
ps:您也可以将工作类信号直接连接到progressbar插槽,您不需要MainWindow::onDisplayStatus(int status)
。
您首先必须在阅读文件时计算进度。从文件中读取输入时,也许最有效地创建进度栏的方法可能会对您有所帮助。
然后,您必须将进度报告给QProgressBar
。为此,您的文件读取方法需要接收进度指标作为功能参数,并调用其状态显示方法。如果您不希望明确依赖QT,那么您可能需要创建一个纯抽象的ProgressIndicator
接口类,使文件阅读方法使用这样的ProgressIndicator
,然后创建一个从ProgressIndicator
和QProgressBar
继承并连接接口方法的类到QProgressBar
中的实现。
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 在qprogressbar中显示进度
- 在 QProgressBar 中显示文本
- 无法显示 QDialog/QProgressbar