Brooks
Brooks

Reputation: 381

Progress bar function not looping

Almost done with that application I've been working on BUT, now I have one more problem. I created a QProgressBar and connected it to a QTimer. It goes up one percent per second but surpasses the actual progress. I have yet to program in the former however I set up the timer to go up one every second. Here is my problem the progress bar goes up to one percent then stops. It hits the if statement every second I know that, but it doesn’t go any higher then 1%.

Edit: Sorry meant to add the code.

#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);
    }

    }

}

If you need more let me know.

Thanks, Brooks Rady

Upvotes: 1

Views: 407

Answers (1)

fbucek
fbucek

Reputation: 1667

You are always incrementing zero. int Count=0; This have to be removed from this function and moved for example to constructor where timer is started and declare it in header file ( shown in last two code snipets )

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
        }
    }
}

You have to declare Count in header file. Count will be stored as long as ThiWindows exists. Not only for a few miliseconds in your example ( Count was destroyed when your UpdateProccess functions finish and then recreated again when it was called again )

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

Count should be initialized before Timer starts

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);

Upvotes: 1

Related Questions