PDA

View Full Version : QProgressBar "crash" - doesn't work properly



metRo_
23rd October 2010, 13:59
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%.


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QProgressBar>

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

public slots:
void test();

private:
QProgressBar *progress;
};

#endif // MAINWINDOW_H


#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)
: QMainWindow(parent)
{

QVBoxLayout *layout = new QVBoxLayout;
progress = new QProgressBar;
layout->addWidget(progress);

QWidget *w=new QWidget();
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)"));
QFile f(dir);
f.open(QIODevice::ReadOnly);
if(f.isOpen())
{
int fileSize=f.size();
int step=fileSize/100;
int bytesProcessed=0;
QString line;
QStringList list;
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!");
}
}

wysota
23rd October 2010, 14:02
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().

metRo_
23rd October 2010, 14:06
It's work now even if i do another things :) Thanks :)


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

wysota
23rd October 2010, 14:10
It's true

Well, it shouldn't work.

metRo_
23rd October 2010, 14:12
Anyway, this "QCoreApplication::processEvents()" is the best way to do what i want or just an easy way?

wysota
23rd October 2010, 14:41
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: Keeping the GUI Responsive.

metRo_
23rd October 2010, 14:56
thanks, i'll read this.