Problem for slow operations in Qt program
-
hi,I 'm working on a program and i have some problems about performance of my projet.
By example, during running of my program, if i want show a widget(to screen) ,it take long time before which widget become visible,or ,i make send a result to user that's make long time too.
I want ,during this long time , show a dialog which
is used to give the user an indication of how long an operation is going to take, and to demonstrate that the application has not frozen
Thanks for advanc -
Did you have a look at "QProgressDialog":http://doc.qt.nokia.com/latest/qprogressdialog.html?
-
If a dialog is taking long to show, you are probably doing too much work in the constructor of the dialog. Instead, try to postphone doing that work untill a little bit later when you have returned to the eventloop again. To keep your UI responsive, it is important not to execute too time consuming operations in the main thread. Instead, either move the operation to a thread of its own, or devide it up into smaller blocks and return to the eventloop regulary. Try to avoid using processEvents() for that though. Instead, considder using a 0-timer to trigger the processing of the next block using a slot, and continue doing that untill your processing queue is empty or you have completed your processing pipeline.
-
I'm trying use Qthread ,but running of my program dont start.
this is class of my object
@
#include "formdp0j_poj.h"
#include "forms0ixmij_x_q0ij.h"
#include "forms1ixmij_x_q1ij.h"
#include "forms2ixmij_x_q2ij.h"
#include"mythread.h"FormdP0j_Poj::FormdP0j_Poj(QWidget *parent) :
QWidget(parent)
{
setupUi(this);
table=new QTableWidget;
table->setRowCount(5);
table->setColumnCount(40);
setupContents();
calculate();
}void FormdP0j_Poj::changeEvent(QEvent *e)
{
QWidget::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
retranslateUi(this);
break;
default:
break;
}
}void FormdP0j_Poj::setupContents()
{//in the constructor's widget,i m doing long operations too
FormS0ixmij_x_q0ij *matrixS0ixmij_x_q0ij=new FormS0ixmij_x_q0ij;FormS1ixmij_x_q1ij *matrixS1ixmij_x_q1ij=new FormS1ixmij_x_q1ij;
FormS2ixmij_x_q2ij *matrixS2ixmij_x_q2ij=new FormS2ixmij_x_q2ij;
tempS0ixmij_x_q0ij= matrixS0ixmij_x_q0ij->getTable();
tempS1ixmij_x_q1ij= matrixS1ixmij_x_q1ij->getTable();
tempS2ixmij_x_q2ij= matrixS2ixmij_x_q2ij->getTable();QVariant variant; QTableWidgetItem * curItem=new QTableWidgetItem; if( (tempS0ixmij_x_q0ij!=0) && (tempS1ixmij_x_q1ij !=0 ) && (tempS2ixmij_x_q2ij !=0) ){ //copie de l'entete int c=1; table->setItem(1,0,new QTableWidgetItem(QString("dP0j/P0j"))); table->setItem(2,0,new QTableWidgetItem(QString("dP1j/P1j"))); table->setItem(3,0,new QTableWidgetItem(QString("dP2j/P2j"))); //table->setItem(4,0,new QTableWidgetItem(QString("q"))); for(int j=1; j < tempS0ixmij_x_q0ij->columnCount() ;j=j+1) { curItem=new QTableWidgetItem; variant= tempS0ixmij_x_q0ij->item(0,j)->data(0); curItem->setData(0,variant); table->setItem(0,c,curItem); c=c+1; } //1ere ligne double coef,a,b; int RCount=tempS0ixmij_x_q0ij->rowCount(); int CCount=tempS0ixmij_x_q0ij->columnCount(); QTableWidgetItem * curItem=new QTableWidgetItem; for( int j =1; j < CCount ;j=j+1 { c=1; coef=0.; for( int i=1 ; j < RCount ;i=i+1 ) { if ((tempS0ixmij_x_q0ij->item(i,j) !=0)){ a= (tempS0ixmij_x_q0ij->item(i,j)->data(0).toDouble()); coef= coef + a; } } curItem=new QTableWidgetItem; curItem->setData(0,coef); table->setItem(1,c,curItem); c=c+1; QCoreApplication::processEvents(); } table->resizeColumnsToContents(); table->resizeRowsToContents(); horizontalLayout->addWidget(table); }
}
void FormdP0j_Poj::calculate()
{
//calcul des coefficients//2 ligne for( int j =1; j < tempS1ixmij_x_q1ij->columnCount() ;j=j+1 ) { c=1; coef=0.; for( int i=1 ; j < tempS1ixmij_x_q1ij->rowCount() ;i=i+1 ) { if ((tempS1ixmij_x_q1ij->item(i,j) !=0)){ a=tempS1ixmij_x_q1ij->item(i,j)->data(0).toDouble(); coef= coef + a; } } curItem=new QTableWidgetItem; curItem->setData(0,coef); table->setItem(2,c,curItem); c=c+1; // QCoreApplication::processEvents(); } //3 ligne for( int j =1; j < tempS1ixmij_x_q1ij->columnCount() ;j=j+1 ) { c=1; coef=0.; for( int i=1 ; j < tempS1ixmij_x_q1ij->rowCount() ;i=i+1 ) { if ((tempS2ixmij_x_q2ij->item(i,j) !=0)){ a=tempS2ixmij_x_q2ij->item(i,j)->data(0).toDouble(); coef= coef + a; } } curItem=new QTableWidgetItem; curItem->setData(0,coef); table->setItem(3,c,curItem); c=c+1; }
}
@and after i 'm calling my widget in a mainwindow's slot
@
void MainWindow::on_actionDP0j_Poj_dP2j_P2j_triggered()
{FormdP0j_Poj *w=new FormdP0j_Poj; int indexNouvelOnglet = onglets->addTab(createPageOnglet(w), tr("Matrix(dP0j/P0j à dP2j/P2j")); onglets->setCurrentIndex(indexNouvelOnglet);
}
@
and the main
@
#include "mainwindow.h"
#include<QPushButton>int main(int argc, char *argv[])
{
QApplication a(argc, argv);MainWindow *mainw=new MainWindow; mainw->show(); return a.exec();
}
@
so ,when i run, program dont respond.
Thanks for advance -
I never tried and don't know it is the right way to go on, but a shortcut to this problem could be put this
@QTimer::singleShot(100, this, setupContents());@
in your constructor. You will need to define setupContents() as a slots and then call calculate(); at the end of setupContents() function.
-
Ok,i know, i dont put in this code, but how do I use QThread, for speed up my program .
Please explain me or give an sample of code.
Thanks for advance.[quote author="mlong" date="1308950226"]Where are you using a threads? It's hard to tell from the code you posted.
Edit: Also, remember, GUI elements (widgets) cannot be in any other thread except the main thread.
[/quote] -
-
[quote author="marcoB" date="1308953599"]I never tried and don't know it is the right way to go on, but a shortcut to this problem could be put this
@QTimer::singleShot(100, this, setupContents());@
in your constructor. You will need to define setupContents() as a slots and then call calculate(); at the end of setupContents() function. [/quote]
[quote author="marcoB" date="1308953599"]I never tried and don't know it is the right way to go on, but a shortcut to this problem could be put this
@QTimer::singleShot(100, this, setupContents());@
in your constructor. You will need to define setupContents() as a slots and then call calculate(); at the end of setupContents() function. [/quote]
I'm trying that,but i have same problems
-
[quote author="maxoreli" date="1309016523"]
[quote author="marcoB" date="1308953599"]I never tried and don't know it is the right way to go on, but a shortcut to this problem could be put this@QTimer::singleShot(100, this, setupContents());@
in your constructor. You will need to define setupContents() as a slots and then call calculate(); at the end of setupContents() function. [/quote]
[quote author="marcoB" date="1308953599"]I never tried and don't know it is the right way to go on, but a shortcut to this problem could be put this
@QTimer::singleShot(100, this, setupContents());@
in your constructor. You will need to define setupContents() as a slots and then call calculate(); at the end of setupContents() function. [/quote]
I'm trying that,but i have same problems
[/quote]
What is the problem ?
You can add QProgressBar, to indicate the status of setupContents()
-
After singleShot call, your GUI should respond to user, even if I noticed now you put some
@QCoreApplication::processEvents();@
in your loops and that could be enough to achieve what you are looking for.
Has Santosh sayd, try to put a progressBar in your form, it should works -
Hello,Andre please I have difficulty to apply what you saw in your first reply.
Please ,read my code and give an sample of code and how do I do it(speed up my program)
Thanks for advance.[quote author="Andre" date="1309070939"]I find that progressEvents, especially the call without any arguments, is a road to very hard to debug issues. I try to avoid it. [/quote]
-
In the code you posted, you are not using threads; at least not where we can see them.
Your main issue is, that you are starting your intensive code from the constructor of your form. As you have been told before, you should not do that. Instead, you could re-write your constructor to something like this:
@
FormdP0j_Poj::FormdP0j_Poj(QWidget *parent) :
QWidget(parent)
{
setupUi(this);
table=new QTableWidget;
table->setRowCount(5);
table->setColumnCount(40);
// trigger heavy work to be done in a little while
QTimer::singleShot(100, this, SLOT(startHeavyWork()));
}//this needs to be a slot in your declaration
FormdP0j_Poj::startHeavyWork()
{
setupContents();
calculate();
}
@That should at least get your form displayed properly, if not responding well yet.
I am guessing your calculate function may take quite a bit of time, right? Assuming that it is indeed this function that takes most time (and not the setupContents function), let's continue our optimization:
So, the calculate function is a candidate to make run asynchronous. That is: it running should not block your GUI. One problem is, is that your current function will impact the GUI (it creates QTableWidgetItems), and is thus not suited to put in a thread. Doing things in your GUI from another thread is not allowed. Lucky for you, the calculate function is an easy candidate to chop into pieces that can be run one after another, and return to the main loop in between.
How you make the split in blocks exactly, depends on your problem. It might take some experimentation. I have no idea how big the data sets you are working with are, and how much time each iteration may take. My goal would be to make sure you return to the eventloop at least 24 times per second, but again: some experimentation would be needed.
Lets start by re-arranging your calculation a bit: you are looping over the exact same two data structures in both the blocks of your calculation. That is a waste of time. Instead, loop over them only one time, and perform both calculations in one go:
@
//calculation, version 2
void FormdP0j_Poj::calculate()
{
//calcul des coefficients// you seem to be missing some variable declarations. Did you cut those out?
//2 ligne for( int j =1; j < tempS1ixmij_x_q1ij->columnCount() ;j=j+1 ) { c=1; coef=0.; //where are these declared? for( int i=1 ; j < tempS1ixmij_x_q1ij->rowCount() ;i=i+1 ) { if ((tempS1ixmij_x_q1ij->item(i,j) !=0)){ a=tempS1ixmij_x_q1ij->item(i,j)->data(0).toDouble(); coef= coef + a; } if ((tempS2ixmij_x_q2ij->item(i,j) !=0)){ a2=tempS2ixmij_x_q2ij->item(i,j)->data(0).toDouble(); coef2= coef2 + a2; } } curItem=new QTableWidgetItem; curItem->setData(0,coef); table->setItem(3,c,curItem); curItem2=new QTableWidgetItem; curItem2->setData(0,coef2); table->setItem(3,c,curItem2); c=c+1; }
}
@Let us assume that each iteration of your outer loop takes roughly 10 to 40ms. That is, after each iteration, we would like to return to the event loop. This is how you could do that:
@
//calculation, version 3
void FormdP0j_Poj::calculate()
{
//calcul des coefficients
m_working = true; // declared as bool class member
m_workCurrentIteration = 0; //declared as int class member
m_workIterationCount = tempS1ixmij_x_q1ij->columnCount(); //also an int memberQTimer::singleShot(0, this, SLOT(doWorkIteration()));
}//declared as slot in the class declaration
void FormdP0j_poj::doWorkIteration()
{
// you seem to be missing some variable declarations. Did you cut those out?int j = m_workCurrentIteration; c=1; coef=0.; //where are these declared? for( int i=1 ; j < tempS1ixmij_x_q1ij->rowCount() ;i=i+1 ) { if ((tempS1ixmij_x_q1ij->item(i,j) !=0)){ a=tempS1ixmij_x_q1ij->item(i,j)->data(0).toDouble(); coef= coef + a; } if ((tempS2ixmij_x_q2ij->item(i,j) !=0)){ a2=tempS2ixmij_x_q2ij->item(i,j)->data(0).toDouble(); coef2= coef2 + a2; } } curItem=new QTableWidgetItem; curItem->setData(0,coef); table->setItem(3,c,curItem); curItem2=new QTableWidgetItem; curItem2->setData(0,coef2); table->setItem(3,c,curItem); c=c+1; }
//prepare for the next iteration
++m_workCurrentIteration;
if (m_workCurrentIteration < m_workIterationCount) {
//this is the trick: we schedule another iteration to be calculated if we need to do more work
QTimer::singleShot(0, this, SLOT(doWorkIteration()));
} else {
m_working = false;
workDone();
}
}void FormdP0j_poj::workDone()
{
// this method is called when all the iterations are done. You might want to update the GUI to reflect that the calculation is done.
}
@A method like this will make sure you return to your eventloop regulary, thus giving your GUI the chance to update. Note that it will not make it run faster. In fact, it will be slower. But it will keep your users more happy. Using a thread might make your code faster, as your code can potentially use more cores in a multi-core machine. However, if you want to use threads, you can not interweave creating QTableWidgetItems into the calculation code. Instead, you'd have to store the results elsewhere. If you want to go that route, I think using QtConcurrent would be a feasable way to go.
disclaimer: all code presented here has just been typed into the editor of the forum. It is not tested, and only intended as an example.
-
The new operator is a very expensive operation to call in a loop. You may get a reasonable improvement by calculating how many QTableWidgetItem's you actually need and new'ing them all in one go. Then in you loop just set the properties of each item as you do now.
Using some sort of profiling tool such as valgrind (on Linux) will tell you where to focus your optimisation efforts.
-
Now,I remove new's operator in my loop,however ,program is slower
[quote author="ZapB" date="1309088968"]The new operator is a very expensive operation to call in a loop. You may get a reasonable improvement by calculating how many QTableWidgetItem's you actually need and new'ing them all in one go. Then in you loop just set the properties of each item as you do now.Using some sort of profiling tool such as valgrind (on Linux) will tell you where to focus your optimisation efforts.[/quote]
-
that's my new method setupContents()
@
void FormdP0j_Poj::setupContents()
{FormS0ixmij_x_q0ij *matrixS0ixmij_x_q0ij=new FormS0ixmij_x_q0ij; FormS1ixmij_x_q1ij *matrixS1ixmij_x_q1ij=new FormS1ixmij_x_q1ij; FormS2ixmij_x_q2ij *matrixS2ixmij_x_q2ij=new FormS2ixmij_x_q2ij; tempS0ixmij_x_q0ij= matrixS0ixmij_x_q0ij->getTable(); tempS1ixmij_x_q1ij= matrixS1ixmij_x_q1ij->getTable(); tempS2ixmij_x_q2ij= matrixS2ixmij_x_q2ij->getTable();
// QCoreApplication::processEvents();
QVariant variant; //QTableWidgetItem * curItem=new QTableWidgetItem; progressDialog.setRange(0,tempS0ixmij_x_q0ij->columnCount() ); progressDialog.setMinimumDuration(0); if( (tempS0ixmij_x_q0ij!=0) && (tempS1ixmij_x_q1ij !=0) && (tempS2ixmij_x_q2ij !=0) ) { //copie de l'entete int c=1; table->setItem(1,0,new QTableWidgetItem(QString("dP0j/P0j"))); table->setItem(2,0,new QTableWidgetItem(QString("dP1j/P1j"))); table->setItem(3,0,new QTableWidgetItem(QString("dP2j/P2j"))); int nbCols=tempS0ixmij_x_q0ij->columnCount() ; //that's there!! QTableWidgetItem *cur=new QTableWidgetItem[nbCols]; for(int j=1; j < nbCols ;j=j+1) { if( tempS0ixmij_x_q0ij->item(0,j)!=0) { //curItem=new QTableWidgetItem; variant= tempS0ixmij_x_q0ij->item(0,j)->data(0); cur[j].setData(0,variant); table->setItem(0,c,&cur[j]); c=c+1; } progressDialog.setValue(j); progressDialog.setLabelText(tr("Processing ...Please wait")); } progressDialog.setValue(nbCols); table->resizeColumnsToContents(); table->resizeRowsToContents(); horizontalLayout->addWidget(table); }
}
@Hello Andre ,how are you. I tried your codes,that's Ok.As you said,my program is always slower.
Now,I decide to remove QTableWidgetItem,I will find a manner to store the results.So ,I want to use Qthreads,we suppose that QTableWidgetItem is not present in code.What will be Code.
Thanks For Advance and excuse me for disturbance...