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>
{
Q_OBJECT
public:
~MainWindow();
public slots:
void test();
private:
};
#endif // MAINWINDOW_H
#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
To copy to clipboard, switch view to plain text mode
#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!");
}
}
#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!");
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks