Results 1 to 9 of 9

Thread: strange behaviour with Qt::key_space

  1. #1
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default strange behaviour with Qt::key_space

    Hi,
    I already posted this thread in the programming section and get no replies.
    I hope ho have more luck here.
    I'm implementing my:

    Qt Code:
    1. void myWidget::keyPressEvent( QKeyEvent* pe )
    2. {
    3. ..code ..
    4. }
    To copy to clipboard, switch view to plain text mode 

    where I catch some keys. I have a problem with the space. I would run a routine when I press the space key so:

    Qt Code:
    1. // start/stop
    2. case Qt::Key_Space:
    3. {
    4. qDebug() << "WaveWidget::keyPressEvent";
    5. bool playing;
    6. m_wave->getCurrentChannel()->isPlaying( &playing );
    7. if( playing == false )
    8. playSound();
    9. else
    10. StopSound();
    11. break;
    12. }
    To copy to clipboard, switch view to plain text mode 

    It simply doesn't work. If I change Key_Space with another key I have no problems.
    Which can be the problem?

    Regards
    Franco Amato

  2. #2
    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: strange behaviour with Qt::key_space

    So what exactly is the "strange behaviour"? You do know that the space key has a special meaning for dialogs and may be intercepted higher in the event processing chain, right?

  3. #3
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: strange behaviour with Qt::key_space

    Quote Originally Posted by wysota View Post
    So what exactly is the "strange behaviour"? You do know that the space key has a special meaning for dialogs and may be intercepted higher in the event processing chain, right?
    Wysota the "strange behaviour" is that "nothing happens".
    So I can not use the space?

  4. #4
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: strange behaviour with Qt::key_space

    No, you can catch the space bar events, just make sure that the myWidget constructor gives the widget strong focus:
    Qt Code:
    1. myWidget::myWidget(QWidget *parent)
    2. {
    3. setFocusPolicy(Qt::StrongFocus);
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    You may also need to call QKeyEvent::accept() but this should not be necessary.

  5. #5
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: strange behaviour with Qt::key_space

    Quote Originally Posted by JD2000 View Post
    No, you can catch the space bar events, just make sure that the myWidget constructor gives the widget strong focus:
    Qt Code:
    1. myWidget::myWidget(QWidget *parent)
    2. {
    3. setFocusPolicy(Qt::StrongFocus);
    4. ...
    5. }
    To copy to clipboard, switch view to plain text mode 

    You may also need to call QKeyEvent::accept() but this should not be necessary.
    Hi I already set the focul policy to strongfocus but nothing change.
    Seems the event is cached in another place

  6. #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: strange behaviour with Qt::key_space

    Quote Originally Posted by franco.amato View Post
    Wysota the "strange behaviour" is that "nothing happens".
    Well... "nothing happens" is the default behaviour so I don't see anything strange in it
    So I can not use the space?
    Sure you can. Reimplement QObject::event() for your class or don't derive from QDialog and remember to give your widget (just make sure it's the right one) some sane focus policy.

  7. #7
    Join Date
    Nov 2007
    Location
    Italy
    Posts
    692
    Thanks
    59
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: strange behaviour with Qt::key_space

    Quote Originally Posted by wysota View Post
    Well... "nothing happens" is the default behaviour so I don't see anything strange in it
    :-) OK

    Sure you can. Reimplement QObject::event() for your class or don't derive from QDialog and remember to give your widget (just make sure it's the right one) some sane focus policy.
    Hi my class derive from QWidget and I set
    Qt Code:
    1. setFocusPolicy( Qt::StrongFocus );
    To copy to clipboard, switch view to plain text mode 
    as focus policy.
    Can you explain me better what I have to reimplement?

  8. #8
    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: strange behaviour with Qt::key_space

    as suggested earlier, reimplement event() or eventFilter().

  9. #9
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: strange behaviour with Qt::key_space

    use like this
    Qt Code:
    1. bool cmylass:: eventFilter(QObject *ob, QEvent *e)
    2. {
    3. if(ob == logout && e->type() == QEvent::KeyPress) {
    4. const QKeyEvent *ke = static_cast<QKeyEvent *>(e);
    5. if(ke->key()==)Qt::Key_Space{
    6. //do your coding std ..
    7. }
    8. return true;
    9. }
    10. return QWidget::eventFilter(ob, e);
    11. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. strange behaviour with Qt key_space
    By franco.amato in forum Qt Programming
    Replies: 0
    Last Post: 9th April 2010, 23:21
  2. Strange mouseReleaseEvent behaviour.
    By ricardo in forum Newbie
    Replies: 11
    Last Post: 5th May 2009, 23:35
  3. Need help: Strange behaviour
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2008, 04:03
  4. very strange behaviour
    By regix in forum Qt Programming
    Replies: 23
    Last Post: 20th July 2006, 17:38
  5. [Qt 4.1] Strange behaviour with QTableView
    By fane in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 06:17

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.