0

I have a class inherits QWizard, and add 2 independent QWizardPage(s). Before go to the next page, I want to do some job (i.e. check internet connection) on the first page. User may click 'next' button by mouse, or directly press enter/return by keyboard since the 'next' button is focused by default. So I install a eventfilter for the button:

button(QWizard::NextButton)->installEventFilter(this);

Then in the wizard class implement the event handling code:

bool MyWizard::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == button(QWizard::NextButton))
    {
        if (currentId() == startPageId)
        {
            if (event->type() == QEvent::KeyPress)
            {
                QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
                if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)
                {
                    // Do something. Cannot be reached.
                }
            }
            else if (event->type() == QEvent::MouseButtonPress)
            {
                // Do something. Can be reached.
            }
        }
    }    
    return QWizard::eventFilter(watched, event);
}

As I tried many times, clicking mouse can always make my event processing code run, but pressing key makes nothing to do. Another curious thing is, press mouse on button without releasing, then move away and release, the wizard stays on first page, now I can press key and all that is OK.

Could any one help to find the reason, is that a Qt bug? how can I make the key event work properly?

Royt
  • 199
  • 4
  • 14
  • Did you try it without the condition if (keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter) – ScarCode May 30 '12 at 11:05
  • I tried, but even this line "QKeyEvent *keyEvent = static_cast(event);" can't be reached. – Royt May 31 '12 at 07:45

1 Answers1

0

Try another simple solution without installing event filter.Use QkeyPressEvent in your main App.

void YourAppClass::keyPressEvent(QKeyEvent *e)
{
     if(Nextbutton->hasFocus() && e->key()== Qt::Key_Return || e->key() == Qt::Key_Enter)
     {
     Do your job//I guarantee this will be reached upon pressing return with button focus
     }
}

You can add mouse-click functionality in nextbutton's clickevent().

ScarCode
  • 3,074
  • 3
  • 19
  • 32
  • Thanks, as I tried, keyPressEvent() will be called. but the line -- "if (button(QWizard::NextButton)->hasFocus())" always returns false, so I can't know if the pressed button is 'Next' button. And if press 'Enter' on 'Cancel' or 'Back' button of wizard, the function keyPressEvent() will not responce. – Royt May 31 '12 at 07:47
  • hasFocus() will return true if it has focus.You can use Tab for traversing between widgets. And also only if nextbutton has focus, return key is meaningful in your context.It will definitely return true if it has focus.This problem remain even if you use eventfilter. – ScarCode May 31 '12 at 08:02
  • I guess that key press event firstly handled by the push button, then propagated up to the parent dialog (my wizard), maybe during the key event processing, the 'next' button has no focus. After arriving at the next page, the 'next' button gets focus again. The question is, when focused item is 'cancel' button or 'back' button, press 'enter' will not cause keyPressEvent() be called, but press any other key the function will be called. Does it mean after the 'Next' button process the enter press event, it will pass the key event to parent dialog? – Royt May 31 '12 at 08:59
  • Keypresssevent is definitely called when you press enter while focus is on cancel or back button, but it doesn't enter the if in keypressevent function as nextbutton doesn't have focus. – ScarCode Jun 01 '12 at 08:30