进度条函数不循环

Progress bar function not looping

本文关键字:循环 函数      更新时间:2023-10-16

我一直在研究的应用程序几乎完成了,但是,现在我还有一个问题。我创建了一个QProgressBar并将其连接到QTimer。它每秒上升百分之一,但超过了实际进度。我还没有在前者中编程,但是我将计时器设置为每秒上升一次。这是我的问题,进度条上升到百分之一然后停止。我知道它每秒都会点击 if 语句,但它并没有高于 1%。

编辑:抱歉,要添加代码。

#include "thiwindow.h"
#include "ui_thiwindow.h"
#include <QProcess>
#include <fstream>
#include <sstream>
int ModeI;
ThiWindow::ThiWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ThiWindow)
{
    ui->setupUi(this);
    std::ifstream ModeF;
    ModeF.open ("/tmp/Mode.txt");
    getline (ModeF,ModeS);
    std::stringstream ss(ModeS);
    ss >> ModeI;
    ModeF.close();
    SecCount = new QTimer(this);
    Aproc = new QProcess;
    proc = new QProcess;
    connect(SecCount, SIGNAL(timeout()), this, SLOT(UpdateProcess()));
    connect(Aproc, SIGNAL(readyRead()), this, SLOT(updateText()));
    connect(proc, SIGNAL(readyRead()), this, SLOT(updateText()));
    SecCount->start(1000);
    if (ModeI==1)
    Aproc->start("gksudo /home/brooks/Documents/Programming/AutoClean/LPB.pyc");
    else
    proc->start("/home/brooks/Documents/Programming/AutoClean/LPB.pyc");
    ui->progressBar->setValue(0);
}
ThiWindow::~ThiWindow()
{
    delete ui;
}

void ThiWindow::updateText()
{
    if (ModeI==1){
    QString appendText(Aproc->readAll());
    ui->textEdit->append(appendText);}
    else{
    QString appendText(proc->readAll());
    ui->textEdit->append(appendText);}
}

void ThiWindow::UpdateProcess()
{
    SecCount->start(1000);
    int Count=0;
    float Increments;
    int Percent_limit;
    if (ModeI==1){
    Increments = 100/5;
    Percent_limit = Increments;
    if (Count<Percent_limit) {
    Count += 1;
    ui->progressBar->setValue(Count);
    }
    }
}

如果您需要更多,请告诉我。

谢谢布鲁克斯·雷迪

您总是在递增零。 整数计数=0;这必须从这个函数中删除,并移动到例如启动计时器的构造函数并在头文件中声明它(显示在最后两个代码狙击手中)

void ThiWindow::UpdateProcess()
{
    SecCount->start(1000);
    int Count=0; // Count is 0
    float Increments;
    int Percent_limit;
    if (ModeI==1){
        Increments = 100/5;
        Percent_limit = Increments;
        if (Count<Percent_limit) {
            Count += 1; // Count is 0 + 1
            ui->progressBar->setValue(Count); // progressBar is 1
        }
    }
}

您必须在头文件中声明 Count。只要 ThiWindows 存在,计数就会被存储。在您的示例中不仅有几毫秒(当您的 UpdateProccess 函数完成时,Count 被销毁,然后在再次调用时再次重新创建)

class ThiWindow : public QMainWindow {
    Q_OBJECT
public:
    // whatever you have
private:
    int Count;
}

计数应在计时器启动之前初始化

SecCount = new QTimer(this);
Aproc = new QProcess;
proc = new QProcess;
connect(SecCount, SIGNAL(timeout()), this, SLOT(UpdateProcess()));
connect(Aproc, SIGNAL(readyRead()), this, SLOT(updateText()));
connect(proc, SIGNAL(readyRead()), this, SLOT(updateText()));
Count = 0; // << move Count variable here
SecCount->start(1000);