Results 1 to 9 of 9

Thread: why does my keyEvent activate after mousepress

  1. #1
    Join Date
    Aug 2015
    Posts
    7
    Thanks
    1

    Default why does my keyEvent activate after mousepress

    My program is base on a stackedWidget and I create a base widget class,all the widget in the stackedWidget are inherite form the base class,I also write a eventFilter to response the key event,codes are below
    :
    void KeyManage::installFilter(QWidget* pwidget)
    {
    qDebug()<<pwidget->objectName();

    widget = pwidget;
    qDebug()<<widget->children().count();
    int a = 0;
    foreach(QObject *obj, widget->children())
    {

    QWidget *item = qobject_cast<QWidget*>(obj);
    qDebug()<<item->objectName();
    if(a==0)
    {
    currentControl =item;
    a=1;
    }
    item->installEventFilter(widget);
    }
    }

    bool KeyManage::setCurrentControl(QWidget* control)
    {
    currentControl = control;
    currentControl->setFocus();
    }

    bool KeyManage::eventFilter(QObject obj, QEvent event)
    {
    QKeyEvent keyEvent;
    if(event->type() == QEvent::KeyPress)
    {
    keyEvent = (QKeyEvent)event;
    if(Qt::Key_Up == keyEvent->key()||
    Qt::Key_Right == keyEvent->key()||
    Qt::Key_Left == keyEvent->key()||
    Qt::Key_Down == keyEvent->key())
    {
    operateForDirectionKey(keyEvent);
    return true;
    }
    else if (Qt::Key_Return == keyEvent->key())
    {
    operateForEnterKey();
    }
    }
    else
    {
    return false;
    }

    return false;
    }
    but when I remove a widget and add a widget to the stackedWidget , when I start the program ,the first widget in the StackedWisget are response right after I press a direction key,but From the second widget ,the direction key has no effect ,I must press the mouse on the Button(only the button,label or table are still no effect),then pressing the direction key will do right.Please give me some advise ,I will very appreciate it .

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: why does my keyEvent activate after mousepress

    Key events are delivered to the focus widget.
    Does the widget get focus when you switch between pages in the stack?

    Also: why an event filter if you derive from the same base class anyway?

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Posts
    7
    Thanks
    1

    Default Re: why does my keyEvent activate after mousepress

    Quote Originally Posted by anda_skoa View Post
    Key events are delivered to the focus widget.
    Does the widget get focus when you switch between pages in the stack?

    Also: why an event filter if you derive from the same base class anyway?

    Cheers,
    _
    THX,I think I did it in a right way.But How can I know the widget get the focus,or what should i do can make the widget get focus ?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: why does my keyEvent activate after mousepress

    Try calling setFocus() in whereever your program switches the pages.

    Cheers,
    _

  5. #5
    Join Date
    Aug 2015
    Posts
    7
    Thanks
    1

    Default Re: why does my keyEvent activate after mousepress

    Quote Originally Posted by anda_skoa View Post
    Try calling setFocus() in whereever your program switches the pages.

    Cheers,
    _
    It is still no effect,I forgotten to say something,when i switch to a new widget ,I must press a control to activate the key event ,even press on the other part of the widget,it still no effect.But I can sure the problem is the widget isnt get the focus or activate,but i have no idea to solve it.Do you think the problem due to the eventFilter written in the base class?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: why does my keyEvent activate after mousepress

    Quote Originally Posted by mike king View Post
    Do you think the problem due to the eventFilter written in the base class?
    Maybe.
    Hard to tell since you didn't post the actual code of your event filter.

    Have you tried with proper key event handling in the base class instead of resorting to a filter?

    Cheers,
    _

  7. The following user says thank you to anda_skoa for this useful post:

    mike king (11th August 2015)

  8. #7
    Join Date
    Aug 2015
    Posts
    7
    Thanks
    1

    Default Re: why does my keyEvent activate after mousepress

    Quote Originally Posted by anda_skoa View Post
    Maybe.
    Hard to tell since you didn't post the actual code of your event filter.

    Have you tried with proper key event handling in the base class instead of resorting to a filter?

    Cheers,
    _
    you meant I write a keyPressEvent() in the base class? But if the KeyPressEvent written in the base class, can the event deliver to the widget from the control,because the event be filtered after the eventFilter,firstly delivered to the relate control,not to the widget.
    Last edited by mike king; 11th August 2015 at 09:45.

  9. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: why does my keyEvent activate after mousepress

    Quote Originally Posted by mike king View Post
    you meant I write a keyPressEvent() in the base class?
    Yes. You said that all your page widgets had the same base class.

    Quote Originally Posted by mike king View Post
    But if the KeyPressEvent written in the base class, can the event deliver to the widget from the control,because the event be filtered after the eventFilter,firstly delivered to the relate control,not to the widget.
    You need an event filter if you need to keep events reaching their actual destinations or if the destination consumes them but you need them as well.

    Maybe that is the case for you.

    Cheers,
    _

  10. #9
    Join Date
    Aug 2015
    Posts
    7
    Thanks
    1

    Default Re: why does my keyEvent activate after mousepress

    Quote Originally Posted by anda_skoa View Post
    Yes. You said that all your page widgets had the same base class.


    You need an event filter if you need to keep events reaching their actual destinations or if the destination consumes them but you need them as well.

    Maybe that is the case for you.

    Cheers,
    _
    please see the code in the first reply,I written the code of eventFilter,I directly changed the focus control after key pressed。

Similar Threads

  1. keyevent
    By mero in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2011, 11:32
  2. Draw shape on mousepress?
    By Niamita in forum Qt Programming
    Replies: 4
    Last Post: 7th June 2011, 07:59
  3. mousepress event
    By tpthyd in forum Qt Programming
    Replies: 3
    Last Post: 15th May 2009, 22:16
  4. mousepress event
    By tpthyd in forum Qt Programming
    Replies: 1
    Last Post: 25th February 2009, 16:02
  5. keyevent/zoom
    By bhogasena in forum Qt Programming
    Replies: 7
    Last Post: 23rd January 2009, 15:28

Tags for this Thread

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.