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

12,998

Solution 1

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

}

Solution 2

QProgressBar has some public slots that are used for setting min and max values and current value. Increasing the current value causes the progress bar to move. You can emit a signal from the logic layer that is connected to "void setValue ( int value )" slot of QProgressBar. http://doc.qt.digia.com/qt/qprogressbar.html

Share:
12,998

Related videos on Youtube

GoldenAxe
Author by

GoldenAxe

Updated on September 17, 2022

Comments

  • GoldenAxe
    GoldenAxe over 1 year

    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

  • GoldenAxe
    GoldenAxe over 11 years
    So to update the progress bar in each iteration of a loop of some method in the logic class, I need to emit signalProgress(int) in each iteration?
  • elsamuko
    elsamuko over 11 years
    Exactly. (stackfill)
  • GTRONICK
    GTRONICK over 7 years
    I'm trying this, it updates the progress bar value correctly, but the main GUI freezes when you have a large process running. I have also tried ui->progressBar->moveToThread(&workerThread), but does not work. How can I avoid GUI freezing? Thanks in advance.
  • elsamuko
    elsamuko over 7 years
    Send the signalProgress() from a worker thread and connect the signal with a Qt::QueuedConnection. I used this here: github.com/elsamuko/copymove2/blob/master/src/ui/….