进度条函数不循环
Progress bar function not looping
我一直在研究的应用程序几乎完成了,但是,现在我还有一个问题。我创建了一个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);
相关文章:
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 循环中的随机函数
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 未在函数C++中执行的循环
- 如何在没有可变参数函数的情况下循环函数的参数
- 循环函数中的并行数组
- 如何循环函数,直到新的 mqtt 消息到达
- 循环C++ R 函数
- 为什么我的 while 循环函数只返回"1"?
- Pthread 循环函数永远不会被调用
- 循环函数调用未正确循环!(C++)
- 对于循环函数参数在C++中的奇数
- 具有双重调度的C++多态循环函数
- 循环函数给出"return’ with a value, in function returning void"误差
- 如何用循环函数求和
- 如何使c++中的for each循环函数与自定义类一起工作
- c++分割故障及循环函数
- C++ |需要帮助从 for 循环函数转换 - >递归函数
- 为什么要在 levelDB 的缓存中使用 while 循环(函数 Resize)?
- odeint中的循环函数