Results 1 to 8 of 8

Thread: QGLWidget, keyPressEvent does not work

  1. #1
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QGLWidget, keyPressEvent does not work

    Hi friends!

    I created a class that derives from QGLWidget, and I rewrote keyPressEvent:

    void CGLEditorRenderer::keyPressEvent(QKeyEvent* e) {
    qDebug("keyPressEvent");
    }

    But if I press a key I never see that message.

    What's going on? Any idea?

    Thanks.

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget, keyPressEvent does not work

    give your glwidget some focus first.. or show us some code

  3. #3
    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: QGLWidget, keyPressEvent does not work

    From a previous post I don't find right now:
    install that event handler in your "QMainWindow" and see to which widget the key event is delivered:
    Qt Code:
    1. bool XXX::eventFilter(QObject *o, QEvent *e)
    2. {
    3. if(e->type() == QEvent::KeyPress)
    4. {
    5. qWarning() << "The bad guy which steals the keyevent is" << o;
    6. }
    7. return false;
    8. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget, keyPressEvent does not work

    Thanks a lot for replies.

    @MrDeath: I guess my opengl componet has focus, because I draw some objects without porblems (actually, I want that if user press ESC, drawing is cancelled)

    @Lykurg: I wrote these lines in my window class:

    Qt Code:
    1. bool CEditor::eventFilter(QObject *obj, QEvent *event) {
    2. if(event->type() == QEvent::KeyPress) {
    3. qDebug(obj->objectName().toAscii());
    4. return true;
    5. } else {
    6. return QObject::eventFilter(obj, event);
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    And this in the main window constructor installEventFilter(this);

    But everytime I press a key, I see CEditorClass, I never see any other string and it doesn't matter if I pressthe key on other object. BTW, it seems SPACE does not work.

    Any idea? Honesly, I don't know what going on is.

    Thanks in advance.

  5. #5
    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: QGLWidget, keyPressEvent does not work

    have you reimp the key press handler also in your class CEditorClass? If so, you need to pass the event through.

  6. #6
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget, keyPressEvent does not work

    are you writing
    qApp->installEventFilter(this);

    simply installEventFilter(this); will not work.

  7. #7
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget, keyPressEvent does not work

    Quote Originally Posted by MrDeath View Post
    are you writing
    qApp->installEventFilter(this);

    simply installEventFilter(this); will not work.
    Thanks Lykurgand MrDeath, it seems we are closer.


    qApp->installEventFilter(this); works better
    Now I see different objects when keyboard is pressed. But never my opengl component, even if I click it and I press any key (in this case, I see eventfilter says another component).
    It's as if my OGL widget never could had focus.

    May I missing something?
    I don't have reimplemented keyPressed in any place.

    By the way, maybe this helps. I'm using Qt Designer, but as it does not have an OpenGL compent I added a horizontal layout, and I added tyhis lines in my window constructor.

    // create opengl widget
    m_gl_drawer=new CGLEditorRenderer(this);
    ui.horizontalLayout_1->addWidget(m_gl_drawer);

    Thanks a lot.

  8. #8
    Join Date
    Apr 2009
    Posts
    132
    Thanks
    67
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QGLWidget, keyPressEvent does not work

    OK. It seems now it works.

    I read this on docs:

    "The policy is Qt::TabFocus if the widget accepts keyboard focus by tabbing, Qt::ClickFocus if the widget accepts focus by clicking, Qt::StrongFocus if it accepts both, and Qt::NoFocus (the default) if it does not accept focus at all."

    And

    "You must enable keyboard focus for a widget if it processes keyboard events. This is normally done from the widget's constructor. For instance, the QLineEdit constructor calls setFocusPolicy(Qt::StrongFocus)."

    After adding setFocusPolicy(Qt::StrongFocus) in my OGL component, it works. It seemed to be focus was disabled.

    Thanks.

  9. The following user says thank you to ricardo for this useful post:

    shenakan (28th September 2009)

Similar Threads

  1. QGraphicsItem keyPressEvent oddness
    By jonks in forum Qt Programming
    Replies: 5
    Last Post: 3rd June 2009, 15:08
  2. Replies: 1
    Last Post: 23rd April 2009, 09:05
  3. Replies: 1
    Last Post: 21st November 2008, 07:00
  4. Replies: 4
    Last Post: 7th May 2008, 00:01
  5. QGLWidget on another QGLWiget
    By showhand in forum Qt Programming
    Replies: 1
    Last Post: 23rd October 2006, 09:59

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.