PDA

View Full Version : is it possible to relate a QprogressDialog or a QProgressBar to a thread's execution?



toufic.dbouk
12th September 2013, 11:04
Hello everyone,
is there a way to connect a QProgressDialog or QProgressBar to the execution of a QThread?
for example the QProgressDailog should appear when the QThread starts and then show the progress until the QThread finishes.

wysota
12th September 2013, 11:16
Does the thread know how much it is left for it to do?

toufic.dbouk
12th September 2013, 11:20
The thread is executing a query so im guessing no it doesn't know how much is left
unless there is an option in QSqlQuery to show the progress of a query being executed

wysota
12th September 2013, 11:37
The thread is executing a query so im guessing no it doesn't know how much is left
So it can't show its progress, can it?

toufic.dbouk
12th September 2013, 11:44
I dont know :P your are the expert here im asking for your assistance
ill try to work on it and get back to you ,
but i was wondering if we could just connect the QThread start method to the the QProgressDialog and show some slow progress
and connect the QThread exit or finish method to the QProgressDialog so that when the thread is done we can just speed up the progress and finish it to 100%

wysota
12th September 2013, 12:35
but i was wondering if we could just connect the QThread start method to the the QProgressDialog and show some slow progress
There is no concept of "progress" with a thread.

If you want to show the user some information that the program is busy then show a progress dialog, set its minimum, maximum and value properties to 0 and show the dialog. When you get a finished signal from the thread, invoke the hide() slot of the dialog.

toufic.dbouk
12th September 2013, 12:44
There is no concept of "progress" with a thread.
i didnt mean progress of the thread , i mean just show some random progress of the QProgressDialog ( as if the bar is just getting filled from 0 to 100 )


If you want to show the user some information that the program is busy then show a progress dialog, set its minimum, maximum and value properties to 0 and show the dialog. When you get a finished signal from the thread, invoke the hide() slot of the dialog.
sounds okay to me , it will do the job in a descent way

Thanks for your help.

anda_skoa
12th September 2013, 14:42
I think you will still need to drive the progress by calling setValue() but that should be easy with a timer.

I.e. like this


class ThreadProgressDialog : public QProgressDialog
{
Q_OBJECT

public:
explicit ThreadProgressDialog(QWidget* parent = 0) : QProgressDialog(parent), m_timer(new QTimer(this)) {
setRange(0, 0);
m_timer->setInterval(500);
connect(m_timer, SIGNAL(timeout()), this, SLOT(advance()));
}

public slots:
void threadStarted() {
m_timer->start();
show();
}

void threadFinished() {
m_timer->stop();
hide();
}

private:
QTimer *m_timer;

private slots:
void advance() {
setValue(value() + 1);
}
};


And then connect the thread's signals to the respective dialog slot.

Cheers,
_

wysota
12th September 2013, 16:07
I suggested using minimum=value=maximum=0 which should get the special behaviour of the progress dialog without the need for setting a value.

anda_skoa
13th September 2013, 08:38
I suggested using minimum=value=maximum=0 which should get the special behaviour of the progress dialog without the need for setting a value.

Are you sure you don't have to do anything else?
I might remember incorrectly but setting the range 0,0 is only switching the mode from "normal prgress" to "bounce left/right, unknown duration", but without calling setValue the bar will not change.

Hence my example using both :)

Cheers,
_

toufic.dbouk
13th September 2013, 09:24
I might remember incorrectly but setting the range 0,0 is only switching the mode from "normal prgress" to "bounce left/right, unknown duration", but without calling setValue the bar will not change.

yea thats exactly the purpose of it to just bounce left and right without stopping , to show the user that something is loading and then just hide or close it when the executing process is done.
your example is great Im sure im gonna use it soon but for now this just gets the job done with 3 lines of code
Thanks for your reply.

wysota
13th September 2013, 09:29
Are you sure you don't have to do anything else?
I might remember incorrectly but setting the range 0,0 is only switching the mode from "normal prgress" to "bounce left/right, unknown duration", but without calling setValue the bar will not change.

Hence my example using both :)


If minimum and maximum are set to both 0, then setting the value to anything other shouldn't have any effect as the value should be bound to min <= val <= max. At least that's my logic :)