[Solved] How to access progressBar from a QWizardPage subclass
-
Here is the error that I got when I try to call:
@
ui->progressBar->setValue(value);
@...from a static function.
The error output:
In static member function 'static void MyQWizard::updateProgressBar(int)':
error: invalid use of member 'MyQWizard::ui' in static member function...This error points to the line 42 of my code which have the follow declaration:
@
private:
Ui::CreateRepoWizard *ui;
@...included "private:" for clarity.
Thanks for help.
-
You cannot access a member variable from a static function: what error do you get when you try to do that from a member function?
-
In the example that I wrote above you can see that the function:
myqwizard.cpp:
@
MyQWizard::MyQWizard(QWidget *parent) : QWizard(parent), ui(new Ui::CreateRepoWizard) {...
}
...
void MyQWizard::updateProgressBar(const int value) {
ui->progressBar->setValue(value);
}
...
@
...is a member of MyQWizard class and as I said, it was declared as a static function:
myqwizard.h:
@
public:
static void updateProgressBar(const int value);
@I need this function be static, to be able to access it from progressPage(a QWizardPage subclass) without creating a new object of MyQWizard :
progresspage.cpp:
@#include <myqwizard.h>
ProgressPage::ProgressPage(QWidget *parent) : QWizardPage(parent) {
...
}
...
MyQWizard::updateProgressBar(value);
...
@Creating this member function as non-static will allow me to update the progressBar without problems, but it implies to create a new object of MyQWizard too, which doesn't have any sense in this scenario. Because I will be using a new instance of MyQWizard rather than the current, therefore I will be updating a different progressBar, that it's not the one that the wizard window shows.
Anyway thanks for the correction.
-
There are a couple approaches you can use here: the first is to emit a signal in ProgressPage that indicates that it would like the progress bar updated, and connect to that in MyQWizard to do the update. A second approach is to get a pointer to MyQWizard from within ProgressPage (is MyQWizard a parent widget, perhaps?). I'd lean towards to first solution for encapsulation reasons, but either will work. Fundamentally, though, you must have an object to operate on: that's why you can't use a static function.
-
As I said, thanks for the correction. I already understood that I can't use a static function. My next step will be try your suggestion, thanks again.
-
Got it!!!
l used the signal-slot approach as you suggested and it worked at the first try. It was so easy that I feel ashamed right now, but well, I guess it's a matter of experience.
Thanks for your continuous help and support, Chris H.
-
What did you use for the signal to emit? Value changed is a signal that is emitted from the progress bar. It looks like from your posting that you wanted to call MyQWizard::updateProgressBar(value); and you said you used a signal-slot solution. I was hoping to see what you used as a solution scheme.
-
I don't have access to the original code right now, so I made an example for you to have an idea how I've solved this.
Note that I've promoted the object "ui->progressBarPage" to "ProgressPage".
Here is the code:
mywizard.h:
@#ifndef MYWIZARD_H
#define MYWIZARD_H#include <QWizard>
namespace Ui {
class MyWizard;
}
class MyWizard : public QWizard
{Q_OBJECT
public:
explicit MyWizard(QWidget *parent = 0); ~MyWizard();
public slots:
void updateProgressBar(const int value);
private:
Ui::MyWizard *ui;
};
#endif // MYWIZARD_H@
mywizard.cpp:
@#include "mywizard.h"
#include "ui_mywizard.h"MyWizard::MyWizard(QWidget *parent) : QWizard(parent), ui(new Ui::MyWizard)
{ui->setupUi(this); ui->progressBar->reset(); connect(ui->progressBarPage, SIGNAL(updateProgressValue(int)), this, SLOT(updateProgressBar(int)));
}// Constructor
MyWizard::~MyWizard()
{delete ui;
}// Destructor
void MyWizard::updateProgressBar(const int value)
{ui->progressBar->setValue(value);
}// Update ProgressBar@
progresspage.h:
@#ifndef PROGRESSPAGE_H
#define PROGRESSPAGE_H#include <QWizardPage>
#include <QTimer>class ProgressPage : public QWizardPage
{Q_OBJECT
public:
explicit ProgressPage(QWidget *parent = 0); void initializePage();
signals:
void updateProgressValue(const int value);
private slots:
void updateValue();
private:
QTimer *timer; int count;
};
#endif // PROGRESSPAGE_H@
progresspage.cpp:
@#include "progresspage.h"
ProgressPage::ProgressPage(QWidget *parent) : QWizardPage(parent)
{timer = new QTimer(this); count = 0; connect(timer, SIGNAL(timeout()), this, SLOT(updateValue()));
}// Constructor
void ProgressPage::initializePage() {
timer->start(100);
}// initializePage
void ProgressPage::updateValue() {
if(count == 100) { timer->stop(); return; }// if count++; emit updateProgressValue(count);
}// updateValue@
Hope this can help you.
-
Absolutely helps. Thanks very much.
-
Glad to know it.