What is the proper way to set QProgressBar to update from the logic layer?

GoldenAxe picture GoldenAxe · Jan 9, 2013 · Viewed 12.8k times · Source

If I want to update a QProgressBar on the view layers from a loop on the logic layer (such as each iteration will update the progress bar), what is the proper way to do that?

Thanks

Answer

elsamuko picture elsamuko · Jan 9, 2013
class LogicClass : public QObject
{
    Q_OBJECT
public:
    explicit LogicClass(QObject *parent = 0);
    int max(){ return 100; }
    int min(){ return 0; }
    void emit50(){ emit signalProgress(50); }

signals:
    void signalProgress(int);

public slots:

};


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    LogicClass logic;

    ui->progressBar->setMaximum( logic.max() );
    ui->progressBar->setMinimum( logic.min() );
    connect( &logic, SIGNAL( signalProgress(int) ), ui->progressBar, SLOT( setValue(int) ) );

    logic.emit50();

}