Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] How to access progressBar from a QWizardPage subclass
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to access progressBar from a QWizardPage subclass

Scheduled Pinned Locked Moved General and Desktop
15 Posts 3 Posters 7.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #6

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris H
      wrote on last edited by
      #7

      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?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #8

        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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris H
          wrote on last edited by
          #9

          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.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #10

            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.

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #11

              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.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                astodolski
                wrote on last edited by
                #12

                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.

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #13

                  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.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    astodolski
                    wrote on last edited by
                    #14

                    Absolutely helps. Thanks very much.

                    1 Reply Last reply
                    0
                    • ? Offline
                      ? Offline
                      A Former User
                      wrote on last edited by
                      #15

                      Glad to know it.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved