PDA

View Full Version : About Progress bar



Thanh Nguyen
13th September 2015, 04:01
I have a class that contains ProgressBar. On another interface when I pressed the button, it will perform the reading of data and pour on the board. I will call it class ProgressBarbar to display the time it made it finished I will close the progress bar behind you. I do not know why sometimes the progress bar was run forever made without closing it. It appears with a low rate. Expect people to explain me, maybe because I use the progress bar wrong. :)

code this:



void TopMenu::onbtLoadData_clicked()
{

if(mDialog==NULL)
{
mDialog= new ThanhDialogTable(NULL);
mDialog->exec();
delete mDialog;
mDialog = NULL;
}
}


class ThanhDialogTable:


ThanhDialogTable::ThanhDialogTable(QWidget *parent):
QDialog(parent),
ui(new Ui::ThanhDialogTable)
{
ui->setupUi(this);
QFuture<void> *m_future = new QFuture<void>;
m_SimpleProgress = new SimpleProgressBar();
m_SimpleProgress->SetContent("Load Data......");
*m_future = QtConcurrent::run(ThanhDialogTable::loadData, m_SimpleProgress);
m_SimpleProgress->exec();
}



and loadData of ThanhDialogTable


bool ThanhDialogTable::loadData(FiSK_SimpleProgressBar *pProgress)
{
/* Here I call to read data to get one list data from the database and then
I put that data into the table and display */
QString select("No, Name1, Name2");
QStringList labels = select.split(", ");
ui->m_table->setColumnCount(labels.count());
ui->m_table->setHorizontalHeaderLabels(labels);
QList<int> columnWidths;
columnWidths << 49 << 129 << 99;
ui->m_table->setColumnWidth(ColumnType::No, columnWidths[ColumnType::No]);
ui->m_table->setColumnWidth(ColumnType::Name1, columnWidths[ColumnType::Name1]);
ui->m_table->setColumnWidth(ColumnType::Name2, columnWidths[ColumnType::Name2]);
QTableWidgetItem *item_name1;
QTableWidgetItem *item_name2 ;
QTableWidgetItem *item_No;
int count = m_ListData.size();
ui->m_table->setRowCount(count);

for (int i = 0; i < count; i++)
{
item_name1 = new QTableWidgetItem ();
item_name2 = new QTableWidgetItem ( );
item_No = new QTableWidgetItem ( );
item_No ->setText(QString::number(i+1));
item_name1->setText(m_ListData.at(i).mName1);
item_name2->setText(m_ListData.at(i).mName2);
ui->m_table->setItem(i,0,item_No);
ui->m_table->setItem(i,1,item_name1);
ui->m_table->setItem(i,2,item_name2);
}

//Close progress bar
pProgress->hide();
pProgress->close();
}


Please help me and sympathetic English in the level. :)

anda_skoa
13th September 2015, 08:44
You are dealing with a multi threaded execution here.
If the loading has finished before the progressbar executes, it will execute and never get the call to close.

The code is also violating the rule to never use UI resources, such as widgets, from a secondary thread.
This could crash at any time.

There is also at least one memory leak: the first QFuture instance.

Cheers,
_

Thanh Nguyen
13th September 2015, 14:36
You are dealing with a multi threaded execution here.
If the loading has finished before the progressbar executes, it will execute and never get the call to close.

The code is also violating the rule to never use UI resources, such as widgets, from a secondary thread.
This could crash at any time.

There is also at least one memory leak: the first QFuture instance.

Cheers,
_

Thank Cheers!
Because I want to have an interface of progess bar is displayed, so I use a class that contains QProgressBar of Qt. You can offer a better solution for me?

anda_skoa
13th September 2015, 17:20
QProgressBar is fine, not related to your problem at all.

Cheers,
_