How to use QProgressBar in Qt

From Qt Wiki
Jump to navigation Jump to search

Template:ArticleMetaData

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);

File:Progressbar1.JPG

Header file[edit | edit source]

  1. ifndef PROGRESSBAR_H
  2. define PROGRESSBAR_H
  1. include <QtGui/QWidget>
  2. include "ui_progressbar.h"
  3. include <QProgressBar>
  4. include <QSpinBox>
  5. include <QVBoxLayout>

class progressbar : public QWidget {

   Q_OBJECT

public: progressbar(QWidget *parent = 0);

   ~progressbar();

private: QWidget* win; QVBoxLayout* layout; QProgressBar* bar; QSpinBox* spin; };

  1. endif // PROGRESSBAR_H


Source file[edit | edit source]

  1. 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.

File:Qtprogress1.jpg

  • Screen shot at 40% progress

File:Qtprogress2.jpg

Example[edit | edit source]

pt:Archived:Como usar uma QProgressBar