我正在使用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。
答案 0 :(得分:0)
请查看文档:{{3}}
您必须设置进度条的最大值和最小值。
答案 1 :(得分:0)
如果您在主线程(Qt GUI的线程)中进行处理,则直到完成后您的小部件才会被更新/重新绘制,然后直接显示100%。
为了正确使用进度条,您需要在其他进程中处理一些代码或异步代码。
PS:您也可以将您的工作者类信号直接连接到进度条插槽,不需要private static final int BINARY = 2; // probably use enum
private static final int OCTAL = 8; // probably use enum
private static final int DECIMAL = 10; // probably use enum
private JTextField textField;
private JButton n1, n2, n3, n4, n5, n6, n7, n8, n9, n0, add, subtract, multiply, divide, clear, backspace, bin, dec, oct, hex;
private JPanel panel, panel1, p;
private int system = DECIMAL;
// initialization code
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == bin){
system = BINARY;
} else if (event.getSource() == dec) {
system = DECIMAL;
} // same for other systems.
if (system == BINARY) {
if(event.getSource() == n1){
textField.setText(textField.getText() + "1");
}
else if(event.getSource() == n0){
textField.setText(textField.getText() + "0");
}
else if(event.getSource() == add){
textField.setText(textField.getText() + " + ");
}
else if(event.getSource() == subtract){
textField.setText(textField.getText() + " - ");
}
else if(event.getSource() == multiply){
textField.setText(textField.getText() + " * ");
}
else if(event.getSource() == divide){
textField.setText(textField.getText() + " / ");
}
else if(event.getSource() == clear){
textField.setText("");
}
} else if (system == DECIMAL) {
if(event.getSource() == n1){
textField.setText(textField.getText() + "1");
}
else if(event.getSource() == n2){
textField.setText(textField.getText() + "2");
}
else if(event.getSource() == n3){
textField.setText(textField.getText() + "3");
}
else if(event.getSource() == n4){
textField.setText(textField.getText() + "4");
}
else if(event.getSource() == n5){
textField.setText(textField.getText() + "5");
}
else if(event.getSource() == n6){
textField.setText(textField.getText() + "6");
}
else if(event.getSource() == n7){
textField.setText(textField.getText() + "7");
}
else if(event.getSource() == n8){
textField.setText(textField.getText() + "8");
}
else if(event.getSource() == n9){
textField.setText(textField.getText() + "9");
}
else if(event.getSource() == n0){
textField.setText(textField.getText() + "0");
}
else if(event.getSource() == add){
textField.setText(textField.getText() + " + ");
}
else if(event.getSource() == subtract){
textField.setText(textField.getText() + " - ");
}
else if(event.getSource() == multiply){
textField.setText(textField.getText() + " * ");
}
else if(event.getSource() == divide){
textField.setText(textField.getText() + " / ");
}
else if(event.getSource() == clear){
textField.setText("");
}
} // same for other systems
}
。
答案 2 :(得分:0)
您首先必须在读取文件时计算进度。也许Most efficient way of creating a progress bar while reading input from a file可以为您提供帮助。
然后,您必须将进度报告给QProgressBar
。为此,您的文件读取方法需要接收进度指示器作为函数参数,并调用其状态显示方法。如果不想显式依赖Qt,则可能要创建一个纯抽象的ProgressIndicator
接口类,使文件读取方法使用这样的ProgressIndicator
,然后创建一个继承自Qt的类。 ProgressIndicator
和QProgressBar
,并将接口方法连接到QProgressBar
中的实现。