Results 1 to 7 of 7

Thread: QWizard: Temporarily disable the Back button

  1. #1
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default QWizard: Temporarily disable the Back button

    Hi all,

    I have a simple QWizard (code below). When the user moves to page 2 of the wizard a longish, asynchronous network request is made. During that request I'd like to disable the Back (and potentially other) buttons on the wizard. When the request is complete the buttons will be enabled and going Back is allowed. Using setEnabled() on the back button is not working, presumably the QWizard code is re-enabling it. I have had to resort to disabling the entire wizard.

    I know that I could set page 1 as a commit page, but that would prohibit the back button entirely on page 2. I only need a temporary block on the back button.

    Have I missed the really obvious way to do this?

    Cheers,
    Chris

    Qt Code:
    1. #include <QtGui>
    2. #include <QDebug>
    3.  
    4. class Page1: public QWizardPage {
    5. Q_OBJECT
    6.  
    7. QLabel *label;
    8. public:
    9. Page1(QWidget *p = 0): QWizardPage(p) {
    10. label = new QLabel(tr("This is an example page 1"), this);
    11. label->setWordWrap(true);
    12.  
    13. QVBoxLayout *layout = new QVBoxLayout;
    14. layout->addWidget(label);
    15. setLayout(layout);
    16. }
    17. };
    18.  
    19. class Page2: public QWizardPage {
    20. Q_OBJECT
    21.  
    22. QLabel *label;
    23. public:
    24. Page2(QWidget *p = 0): QWizardPage(p) {
    25. label = new QLabel(tr("This is an example page 2"), this);
    26. label->setWordWrap(true);
    27.  
    28. QVBoxLayout *layout = new QVBoxLayout;
    29. layout->addWidget(label);
    30. setLayout(layout);
    31. }
    32. void initializePage() {
    33. // This does not disable the button for the long process
    34. wizard()->button(QWizard::BackButton)->setEnabled(false);
    35. // so I have to resort to this
    36. // wizard()->setEnabled(false);
    37.  
    38. label->setText("Starting");
    39. // simulate a longish process
    40. QTimer::singleShot(5000, this, SLOT(expired()));
    41. }
    42. public slots:
    43. void expired() {
    44. label->setText("Finished");
    45. wizard()->button(QWizard::BackButton)->setEnabled(true);
    46. // wizard()->setEnabled(true);
    47. }
    48. };
    49.  
    50. class Wizard : public QWizard {
    51. Q_OBJECT
    52. public:
    53. Wizard(QWidget *p = 0) : QWizard(p) {
    54. addPage(new Page1);
    55. addPage(new Page2);
    56. }
    57.  
    58. };
    59.  
    60.  
    61. int main(int argc, char *argv[])
    62. {
    63. QApplication app(argc, argv);
    64.  
    65.  
    66. Wizard wiz;
    67. wiz.show();
    68. return app.exec();
    69. }
    70. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QWizard: Temporarily disable the Back button

    Wow, that's crazy. QWizard seems to set the buttons at last. Even the signal currentIdChanged() is useless. The only workaround is to create a slot which disables the button and call that slot inside initializePage() with an 1ms single shot timer. That's not nice, but the only solution I can figure out right now.

  3. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QWizard: Temporarily disable the Back button

    QWizardPage::initializePage() is virtual function is called by QWizard::initializePage() to prepare the page just before it is shown, once the page is prepared the control goes back to QWizard, then QWizard sets the Back and Next button status based on the page options set in QWizardPage::initializePage(), so it may not be good idea to set buttons in there.

    You can set the buttons sometime later, something like this

    Qt Code:
    1. class Page2: public QWizardPage {
    2. Q_OBJECT
    3.  
    4. QLabel *label;
    5. public:
    6. Page2(QWidget *p = 0): QWizardPage(p) {
    7. label = new QLabel(tr("This is an example page 2"), this);
    8. label->setWordWrap(true);
    9.  
    10. QVBoxLayout *layout = new QVBoxLayout;
    11. layout->addWidget(label);
    12. setLayout(layout);
    13. }
    14. void initializePage() {
    15. label->setText("Starting");
    16. // set trigger for longish process
    17. QTimer::singleShot(0, this, SLOT(start()));
    18. }
    19. public slots:
    20. void start() {
    21. wizard()->button(QWizard::BackButton)->setEnabled(false);
    22. // simulate a longish process
    23. QTimer::singleShot(5000, this, SLOT(expired()));
    24. }
    25.  
    26. void expired() {
    27. label->setText("Finished");
    28. wizard()->button(QWizard::BackButton)->setEnabled(true);
    29. }
    30. };
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to Santosh Reddy for this useful post:

    ilermar (9th April 2012)

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QWizard: Temporarily disable the Back button

    I guess the zero-duration single shot timer is an option. A bit clunky IMHO.

    Edit: You also need to be careful not to emit completeChanged() or QWizard will reset your buttons (all of them, not just the Next/Finished button)
    Last edited by ChrisW67; 3rd June 2011 at 08:01.

  6. #5
    Join Date
    Sep 2008
    Posts
    25
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: It is possible to completely hide Back button

    Hello Guyz,

    I've looked at Qt's sources and found out that it's possible to hide Back button by creating cusom button layout and ommiting Back button in the list:
    Qt Code:
    1. QList<QWizard::WizardButton> button_layout;
    2. button_layout << QWizard::HelpButton << QWizard::Stretch <<
    3. QWizard::NextButton << QWizard::CustomButton1 <<
    4. QWizard::CancelButton;
    5. this->setButtonLayout(button_layout);
    To copy to clipboard, switch view to plain text mode 

    I hope this will save some time to somebody.

    Regards


    Added after 7 minutes:


    AFAIU to avoid using QTimer it is needed to modify QWizard source code. The easies way will be to add a virtual function
    virtual void buttonsUpdated();
    and call it from the end of QWizard's:
    void QWizardPrivate::_q_updateButtonStates()
    Then reimplement this buttonsUpdated() in your QWizard sublass and disable Back button there.
    Last edited by DIMEDROLL; 31st January 2012 at 00:00.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: It is possible to completely hide Back button

    Adding virtual functions to public classes is no fun as it breaks binary compatibility.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Sep 2008
    Posts
    25
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: It is possible to completely hide Back button

    wysota, thanks for mentioning about this.
    The binary compatibility issue can be fixed using a signal instead of virtual function:
    documentation

Similar Threads

  1. detecting back and forward buttons on >= 5 button mouse
    By chezifresh in forum Qt Programming
    Replies: 3
    Last Post: 4th February 2011, 20:45
  2. How to move back several pages in QWizard?
    By viet.nguyentien in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2010, 04:03
  3. Replies: 2
    Last Post: 2nd March 2009, 17:11
  4. How to disable NextButton in QWizard ?
    By litroncn in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2008, 07:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.