1

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

    }

}

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

谢谢,布鲁克斯拉迪

4

1 回答 1

1

你总是递增零。整数计数=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;
}

应在 Timer 启动之前初始化计数

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);
于 2013-09-18T06:36:08.233 回答