进度条功能不循环

时间:2013-09-17 23:00:19

标签: c++ qt loops time progress-bar

几乎完成了那个应用程序,我一直在努力,但现在我还有一个问题。我创建了一个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);
    }

    }

}

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

谢谢, 布鲁克斯拉迪

1 个答案:

答案 0 :(得分:1)

您总是递增零。 int Count = 0;这必须从此函数中删除,并移动到例如启动计时器的构造函数,并在头文件中声明它(在最后两个代码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
        }
    }
}

您必须在头文件中声明Count。只要ThiWindows存在,Count就会被存储。不仅仅是你的例子中的几毫秒(当你的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);