How to use QProgressBar in Qt
Overview[edit | edit source]
This code snippet demonstrates how to use a QProgressBar in Qt.
Various Functions[edit | edit source]
- This display the current completed percentage in progress bar.
bar->setVisible(1);
File:Qtprogress2.jpg
- This property is used to invert the direction of the progress in progress bar.
bar->setInvertedAppearance(1);
File:SpinInvert.JPG
- This function is used for the orientation of progress bar.
bar->setOrientation(Qt::vertical);
Header file[edit | edit source]
- ifndef PROGRESSBAR_H
- define PROGRESSBAR_H
- include <QtGui/QWidget>
- include "ui_progressbar.h"
- include <QProgressBar>
- include <QSpinBox>
- include <QVBoxLayout>
class progressbar : public QWidget
{
Q_OBJECT
public:
progressbar(QWidget *parent = 0);
~progressbar();
private:
QWidget* win;
QVBoxLayout* layout;
QProgressBar* bar;
QSpinBox* spin;
};
- endif // PROGRESSBAR_H
Source file[edit | edit source]
- include "progressbar.h"
progressbar::progressbar(QWidget *parent)
: QWidget(parent)
{
win = new QWidget(this);
win->setWindowTitle(("LCD Number"),this);
layout = new QVBoxLayout(this);
bar = new QProgressBar(this);
spin = new QSpinBox(this);
spin->setMaximum(99);
spin->setMinimum(0);
bar->resize(200,25);
bar->setOrientation(Qt::Horizontal);//Orientation can also be vertical
bar->setRange(0,99);
connect(spin, SIGNAL(valueChanged(int)), bar, SLOT(setValue(int)));
layout->addWidget(spin,Qt::AlignCenter);
layout->addWidget(bar,Qt::AlignCenter);
win->setLayout(layout);
win->showMaximized();
}
progressbar::~progressbar()
{
// No need to delete any object that has a parent which is properly deleted.
}
PostConditions[edit | edit source]
- Here as you change the value of the spinbox progress bar will also changed.
- Screen shot at 40% progress
Example[edit | edit source]
- Download the working example from this link: Progressbar.zip
pt:Archived:Como usar uma QProgressBar