Search code examples
c++qtqwidget

How to disable next button in QWizard


What I'm trying to do

I am trying to create a subclass of QWizardPage that looks somewhat like this,but has a slight tweak. I want to disable the next button when a counter variable is more than 0. (It can't be 0 from the get-go due to some functionality that requires it to go x..x-1...0).

What I've tried

Reimplement isComplete() and emit completeChanged() in the constructor

bool DemoWizardPage::isComplete()
{
  return ! (counter > 0); //Also tried just return false;
}

Reimplement initializePage and disable the next button from there

void DemoWizardPage::initializePage()
{
  qDebug() << "QWizardPage:: initialize page";

  if (!this->isComplete())
  {
    qDebug() << "try to turn off next button";
    wizard()->button(QWizard::NextButton)->setDisabled(true);

    qDebug() << "next button enabled? "
             << wizard()->button(QWizard::NextButton)->isEnabled();
  }
}

Results so far

From stepping through the code I can see that the next button is disabled when the page loads. But then it is enabled again due to these 2 lines in QWizardPrivate (taken from qwizard.cpp)

 bool complete = page && page->isComplete();
 btn.next->setEnabled(canContinue && complete);

I am quite baffled as to why isComplete() is returning true here. I mean, I set my counter to be 2 at the beginning and I never decrease it. (And yes, I do emit a completeChanged() whenever I set the counter).

Any ideas?


Solution

  • QWizard automatically manages the state of Next button based on the QWizardPage::isComplete(), you should not implement that functionality yourself in initializePage(). The reason your isComplete() is not being called is that it doesn't actually overwrite QWizardPage::isComplete const from QWizardPage. Declare your function const and it will properly overwrite the original function.