PDA

View Full Version : Show an indicator icon after a heavy process



qt_developer
10th September 2013, 19:09
Hi all,

I want to show an indicator icon before a heavy process. I've used the next code:



void myClass::heavyProcess(QString path)
{
setCursor(Qt::WaitCursor);
QApplication::processEvents();
// heavy process implementation
setCursor(Qt::ArrowCursor);
}


But the indicator icon does not show. I think that the reason is because the code is synchronous. Therefore I have used this other implementation:



void myClass::start_heavyProcess(QString path)
{
setCursor(Qt::WaitCursor);
QApplication::processEvents();
m_path = path; // I have added a class attribute to comunicate the value to heavyProcess() method. It is very dirty...
QTimer::singleShot(0, this, SLOT(on_heavyProcess());
}

void myClass::on_heavyProcess()
{
heavyProcess(m_path);
}

void myClass::heavyProcess(QString path)
{
// heavy process implementation
setCursor(Qt::ArrowCursor);
}


But the indicator icon does not show all the times and the way to pass parameter to the heavyProcess() method is very bad.

How can I improve the code?

Best regards.

anda_skoa
11th September 2013, 09:15
Regarding passing the parameter



void myClass::start_heavyProcess(QString path)
{
//....

QMetaObject::invokeMethod( this, "heavyProcess", Qt::QueuedConnection, Q_ARG( QString, path ) );
}


No idea why your cursor doesn't change though :(
Does it change if you do not execute the heavyProcess method?

Cheers,
_

qt_developer
11th September 2013, 11:37
Hi anda_skoa,

I've used QMetaObject::invokeMethod() instead of QTimer::singleShot(). Is it possible to set a time delay like in QTimer::singleShot()?

Yes, if I do not execute the heavyProcess method, the cursor changes.

Best regards.

anda_skoa
11th September 2013, 14:47
I've used QMetaObject::invokeMethod() instead of QTimer::singleShot(). Is it possible to set a time delay like in QTimer::singleShot()?

No, it only delays (that is what Qt::QueuedConnection does) the invocation, i.e. like QTimer::singleShot() with 0 timeout.
If you need a different timeout, you will need QTimer.




Yes, if I do not execute the heavyProcess method, the cursor changes.

Interesting. Your attempt of delaying the blocking processing looks correct to me.

Cheers,
_

qt_developer
11th September 2013, 23:14
Hi,

I've solved the problem. I've changed the the code in my first attempt and works properly now:



void myClass::heavyProcess(QString path)
{
QApplication::setOverrideCursor(Qt::WaitCursor);
QApplication::processEvents();
// heavy process implementation
QApplication::restoreOverrideCursor();
}


Best regards.