QWizard : clear focus Next Button



  • Hello,

    I would like to know how to clear the focus in the "Next" button on a QWizard page.

    Any idea ?

    Thanks.


  • Qt Champions 2016

    hmmmm.
    Maybe you can set focus to something else in code?
    But why you want to do it?

    validatePage() can be used to control if user can press next.
    Or QWizardPage::isComplete() to disable it.



  • @mrjj

    how to set validatePage() to false ?


  • Qt Champions 2016

    @fgdevel

    It is a virtual function so you must create your own page child class to set it.
    and then use MyPage in the wizard.
    Something like

    #include <QWizard>
    #include <QWizardPage>
    
    class MyPage : public QWizardPage{
    public:
      MyPage(QWidget* parent=0):QWizardPage(parent){  }
      virtual bool validatePage()(){return false;}
    };
    


  • @mrjj

    with your code, when I call validatePage(), the Next Button is already focused...


  • Qt Champions 2016

    @mrjj said:

    validatePage

    This virtual function is called by QWizard::validateCurrentPage() when the user clicks Next or Finish to perform some last-minute validation. If it returns true, the next page is shown (or the wizard finishes); otherwise, the current page stays up.

    So I assumed that is what you wanted?



  • @mrjj

    not at all.
    I want to see the QWizard page but with the focus disabled on the Next button


  • Qt Champions 2016

    @fgdevel
    Ok it means user can still just click on it ?

    You can just set focus to something then using
    setFocus();
    Like one of your own buttons or a textedit.



  • @mrjj

    Yes, it's just the focus I want to remove.
    Because I use i QWizard page, it's not simple


  • Qt Champions 2016

    @fgdevel
    By remove focus you mean so that if user hit say enter, then nothing happens?
    Or what is thee goal of what you want?



  • @mrjj

    yes it's the goal. When the user press Enter key, nothing happens


  • Qt Champions 2016

    @fgdevel
    but that is 100% what validatePage does.
    If you return false, the Next dont work/nothing happens.
    Dont matter if you hit enter or click on it.

    So that is why im confused of what you really mean.



  • @mrjj

    I want Next button works. But just focus not


  • Qt Champions 2016

    @fgdevel
    ok so you want user to click on it.

    Have a look at setFocusProxy();

    http://ldc.usb.ve/docs/qt/dialogs-complexwizard.html

    It seems that you can call that in a Pages constructor and then it will give focus to that widget when page is
    shown.



  • @mrjj

    I have used diffrent setFocus functions but none of them seems to work...


  • Qt Champions 2016

    @fgdevel
    Hi
    You are right. by magic it can still hit enter on Next...

    I changed sample "trivialwizard" to use eventfilter.
    That can eat the enter key. :)
    so key changes are
    class myEventFilter: public QObject // the event filer
    app.installEventFilter(new myEventFilter()); // using the filter

    #include <QtWidgets>
    #include <QTranslator>
    #include <QLocale>
    #include <QLibraryInfo>
    QWizard* wizardp;
    QWizardPage* createIntroPage() {
      QWizardPage* page = new QWizardPage;
      page->setTitle("Introduction");
    
      QLabel* label = new QLabel("This wizard will help you register your copy "
                                 "of Super Product Two.");
      label->setWordWrap(true);
      QLineEdit* nameLineEdit = new QLineEdit;
    
      QVBoxLayout* layout = new QVBoxLayout;
      layout->addWidget(label);
      layout->addWidget(nameLineEdit);
      page->setLayout(layout);
    
    
      wizardp->button(QWizard::CancelButton)-> setFocus();
      return page;
    }
    
    QWizardPage* createRegistrationPage()
    {
      //! [3]
      QWizardPage* page = new QWizardPage;
      page->setTitle("Registration");
      page->setSubTitle("Please fill both fields.");
    
      QLabel* nameLabel = new QLabel("Name:");
      QLineEdit* nameLineEdit = new QLineEdit;
    
      QLabel* emailLabel = new QLabel("Email address:");
      QLineEdit* emailLineEdit = new QLineEdit;
    
      QGridLayout* layout = new QGridLayout;
      layout->addWidget(nameLabel, 0, 0);
      layout->addWidget(nameLineEdit, 0, 1);
      layout->addWidget(emailLabel, 1, 0);
      layout->addWidget(emailLineEdit, 1, 1);
      page->setLayout(layout);
    
      return page;
      //! [4]
    }
    //! [2] //! [4]
    
    //! [5] //! [6]
    QWizardPage* createConclusionPage()
    //! [5] //! [7]
    {
      //! [7]
      QWizardPage* page = new QWizardPage;
      page->setTitle("Conclusion");
    
      QLabel* label = new QLabel("You are now successfully registered. Have a "
                                 "nice day!");
      label->setWordWrap(true);
    
      QVBoxLayout* layout = new QVBoxLayout;
      layout->addWidget(label);
      page->setLayout(layout);
    
      return page;
      //! [8]
    }
    //! [6] //! [8]
    // this you need
    class myEventFilter: public QObject {
     public:
      myEventFilter(): QObject() {}
      ~myEventFilter() {}
      bool eventFilter(QObject* object, QEvent* event) {
        if (event->type() == QEvent::KeyPress) {
          QKeyEvent* key = static_cast<QKeyEvent*>(event);
          if ( (key->key() == Qt::Key_Enter) || (key->key() == Qt::Key_Return) ) {
            event->ignore();
          } else {
            return QObject::eventFilter(object, event);
          }
        }
      }
    };
    
    //! [9] //! [10]
    int main(int argc, char* argv[])
    //! [9] //! [11]
    {
      QApplication app(argc, argv);
    
    #ifndef QT_NO_TRANSLATION
      QString translatorFileName = QLatin1String("qt_");
      translatorFileName += QLocale::system().name();
      QTranslator* translator = new QTranslator(&app);
      if (translator->load(translatorFileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
        app.installTranslator(translator);
    #endif
    
      QWizard wizard;
      wizardp = &wizard;
      wizard.addPage(createIntroPage());
      wizard.addPage(createRegistrationPage());
      wizard.addPage(createConclusionPage());
    
      wizard.setWindowTitle("Trivial Wizard");
      wizard.show();
      app.installEventFilter(new myEventFilter()); // install filter
      return app.exec();
    }
    //! [10] //! [11]
    


  • @mrjj

    I've found a solution with :

    this->setOptions(QWizard::NoDefaultButton);
    this->setOption(QWizard::NoDefaultButton, true);


  • Qt Champions 2016

    @fgdevel
    super.
    Thank you for reporting back.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.