PDA

View Full Version : How can I have a Progressbar with movement



roseicollis
7th January 2015, 15:11
Hi,

I want to have a progressbar with some movement on it so the user can be able to know if the application is responding and wroking or not.

Something like this being a pbar: http://www.telex.com/binary/mapLoadBar.gif.pagespeed.ce.2tpbbkj2Zu.gif

Any idea about if Qt allows you to do it?

Thanks!

anda_skoa
7th January 2015, 18:09
Some platforms have an animation for progressbars even when there is no change to the actual value.

If you want a progressbar that shows progress for an unknown "total", set both minimum and maximum to 0 and just increment the value on each step.

Cheers,
_

roseicollis
8th January 2015, 09:10
Hi,

If I put

ui->pBar->setMinimum(0);
ui->pBar->setMaximum(0);
and then when I want I put: ui->pBar->setValue(value); incrementing the value everytime I call this line

then the pbar is static and does nothing, not movement and not increment.

sulliwk06
8th January 2015, 13:03
my first thought would be to make sure your event loop is running while you are incrementing the value

anda_skoa
8th January 2015, 13:27
Yes, this definitely works.



#include <QtWidgets>

int main(int argc, char ** argv)
{
QApplication app(argc, argv);

QProgressBar bar;
bar.setMinimum(0);
bar.setMaximum(0);
bar.resize(300, bar.sizeHint().height());
bar.show();

QTimer timer;
QObject::connect(&timer, &QTimer::timeout, [&bar]() { bar.setValue(bar.value() + 1); });
timer.start(500);

return app.exec();
}



TEMPLATE = app

QT += widgets
CONFIG += c++11

SOURCES = progressbartest.cpp

nish
9th January 2015, 05:49
Looks like your gui thread is busy doing stuff and cant update the progressbar. So either move your logic to another thread or call processEvents somewhere periodically.

roseicollis
9th January 2015, 13:02
Hi,

anda_skoa thanks for your time. About your answer.. that code gaves me so much problems.

Obviously I don't have this cpp... dunno for what is that line but I didnt wrote it:

SOURCES = progressbartest.cpp

But apart of that it says that:
void QTimer::timeout() is protected
and
no matching function for call to 'QObject::connect(QTimer*, void (QTimer::*)(),miain(int, char**)::<lambda()>)'

Im using this version of Qt over unix:

Qt Creator 2.8.0
Based on Qt 4.8.5

wysota
9th January 2015, 13:22
Im using this version of Qt over unix:

Qt Creator 2.8.0
Based on Qt 4.8.5

I suggest you update your forum user profile as it says you are using Qt5. The code anda_skoa provided will not work with Qt4 as it is using a lambda expression. I'm not sure why you'd run it though, if he says it works then you should focus on what is different in your own code.

roseicollis
9th January 2015, 15:03
Nish I already have 2 threads, one for GUI and another for the background job... but I must be doing something wront

wysota, totally agree with u, I have to apologize to anda_skoa.. I thought I was using Qt5 until I wanted to verify it and saw that -.-'' I've tried to use it just like
mybar.setMinimum(0);
mybar.setMaximum(0);

but then the thread stops because I have a gif on it (until I get that pbar with movement) and it stops moving....

anda_skoa
9th January 2015, 16:05
Obviously I don't have this cpp... dunno for what is that line but I didnt wrote it:

SOURCES = progressbartest.cpp

That is obviously the C++ code I've posted.



Based on Qt 4.8.5

Works the same way in Qt4 as far as the progressbar is concerned.



#include <QtGui>

class Advancer : public QObject
{
Q_OBJECT
public:
explicit Advancer(QProgressBar *bar) : QObject(bar), mBar(bar)
{
connect(&mTimer, SIGNAL(timeout()), this, SLOT(advance()));
}

void start(int interval)
{
mTimer.start(interval);
}

private:
QProgressBar *mBar;
QTimer mTimer;

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

int main(int argc, char ** argv)
{
QApplication app(argc, argv);

QProgressBar bar;
bar.setMinimum(0);
bar.setMaximum(0);
bar.resize(300, bar.sizeHint().height());
bar.show();

Advancer *advancer = new Advancer(&bar);
advancer->start(500);

return app.exec();
}

#include "progressbartestqt4.moc"




TEMPLATE = app

SOURCES = progressbartestqt4.cpp


Cheers,
_