QProgressBar "crash" - doesn't work properly
I'm usign a QProgressBar to know the state of a job that consist in read a file.
My problem is if during that process i click in anything or open anothers aplications, my aplication act like it crashed and just show 100% when the process finish, like: 1%, 2%, i click, ------------------ 100%.
If i just run and didn't do nothing in my pc the QProgressBar show the process correct, like, 1%, 2%, 3%, 4%, ..., 99%, 100%.
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QProgressBar>
{
Q_OBJECT
public:
~MainWindow();
public slots:
void test();
private:
};
#endif // MAINWINDOW_H
Code:
#include "mainwindow.h"
#include <QDebug>
#include <QString>
#include <QDir>
#include <QFileDialog>
#include <QMessageBox>
#include <QLabel>
#include <QProgressBar>
#include <QLayout>
#include <QVBoxLayout>
#include <QTimer>
#include <QProgressDialog>
MainWindow
::MainWindow(QWidget *parent
){
layout->addWidget(progress);
w->setLayout(layout);
this->setCentralWidget(w);
QTimer::singleShot(100,
this,
SLOT(test
()));
}
MainWindow::~MainWindow()
{
}
void MainWindow::test(){
QString dir
= QFileDialog::getOpenFileName(this, tr
("Select Text File"),
"",tr
("Text (*.txt)"));
if(f.isOpen())
{
int fileSize=f.size();
int step=fileSize/100;
int bytesProcessed=0;
progress->setMaximum(fileSize);
progress->setValue(bytesProcessed);
while (not f.atEnd()){
line = f.readLine().data();
bytesProcessed+= line.size();
list = line.split(":");
if (bytesProcessed%step<=20){
//qDebug()<<"bytesProcessed:"<<bytesProcessed;
progress->setValue(bytesProcessed);
}
}
progress->setValue(bytesProcessed);
}
else
{
QMessageBox::warning(this,
"Error",
"No file selected!");
}
}
Re: QProgressBar "crash" - doesn't work properly
To be honest I'm suprised that if you don't do anything then it works correctly. You have a while loop that doesn't allow events to be processed so the progress bar shouldn't get updated. At least add a QCoreApplication::processEvents() call after each call to QProgressBar::setValue().
Re: QProgressBar "crash" - doesn't work properly
It's work now even if i do another things :) Thanks :)
Quote:
To be honest I'm suprised that if you don't do anything then it works correctly.
It's true, i can record a video if you want to see :s
Re: QProgressBar "crash" - doesn't work properly
Quote:
Originally Posted by
metRo_
It's true
Well, it shouldn't work.
Re: QProgressBar "crash" - doesn't work properly
Anyway, this "QCoreApplication::processEvents()" is the best way to do what i want or just an easy way?
Re: QProgressBar "crash" - doesn't work properly
It's an easy way. But for such a simple operation like this it is ok. If you start doing something besides reading from file then some other approach might be better. For this situation the only change I could advise would be to read the file using a thread so that the main part of the application can do other things while the data is being loaded.
In general read this: [wiki]Keeping the GUI Responsive[/wiki].
Re: QProgressBar "crash" - doesn't work properly