PDA

View Full Version : Progress bar Vs loop...



Rakma74
28th September 2012, 20:02
Dear Sirs,

I'm filling large Qtable with large data files, and I decided to add a progress bar.
This works properly, however, I decided to add a 'Cancel' button, and things became more complicated.

Indeed, during the data filling (for loops), the program is completely frozen, and no way to move windows, click on the Cancel button ....

Would there be any solution which would help me ?

Thanks a lot in advance,

Best Regards,

Stéphane




// PROGRESS BAR -----------------------------------

// Main window
QWidget *window_progress_bar = new QWidget;

// Progress bar + button
QProgressBar *progressbar = new QProgressBar;
QPushButton *button = new QPushButton ("Cancel");

// Layout
QHBoxLayout *layout = new QHBoxLayout;

// Layout assignment
layout->addWidget(progressbar);
layout->addWidget(button);

// Set Layout
window_progress_bar->setLayout(layout);

// Show the window
window_progress_bar->show();

// ------------------------------------------------


// Telemetry data importation and filling
for (unsigned int i=0; i<data->get_row_nb(); i++)
{
for (unsigned int j=0; j<data->get_col_nb(); j++)
{
data->set_value(i, j, file_reader->get_data(i,j));
QTableWidgetItem *newItem = new QTableWidgetItem( tr("%1").arg(data->get_value(i,j)) );
// newItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
table_data->setItem(i, j, newItem);
}

// Progress bar updating
progressbar->setValue(100*(i+1)/(data->get_row_nb()));
}

code_err
28th September 2012, 21:38
Maybe multitasking and QThreads?

wysota
28th September 2012, 21:47
Try QProgressDialog.

Rakma74
29th September 2012, 14:13
Thanks code_err, this is exactely what I needed...


In fact, even if I don't use the QProgressBar, the code is frozen during the 'for' loops...
No particular problem with this in the past, but now, since I want to use a 'Cancel' button, things are different...

Best Regards,

Stéphane


PS : Also, thanks to Wysota

boudie
30th September 2012, 20:34
If you want to keep your program resposive, then call qApp->processEvents(); inside the loop.

Rakma74
1st October 2012, 00:13
This is absolutely PERFECT !!

Exactely what I needed... I tested it, it works perfectly and is so simple to implement...

Thank you very much Boudie !!