Results 1 to 9 of 9

Thread: Key Press Event Handling

  1. #1
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Key Press Event Handling

    I know, there is more thread about that in this forum. I looked all of them, but still i cant work it.

    Vista Business
    Qt SDK 2010.05
    Qt Creator 2.0.1

    I'm just trying to get which key is pressed.

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include "ui_mainwindow.h"
    5.  
    6. class MainWindow : public QMainWindow, private Ui::MainWindow
    7. {
    8. Q_OBJECT
    9. private:
    10. Ui::MainWindow *gui;
    11.  
    12. public:
    13. explicit MainWindow(QWidget *parent = 0);
    14.  
    15. protected:
    16. bool keypres(QKeyEvent *keyevent);
    17.  
    18. };
    19.  
    20. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 



    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QKeyEvent>
    3. #include <QDebug>
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),gui(new Ui::MainWindow){
    7. gui->setupUi(this);
    8.  
    9. gui->textEdit->installEventFilter(this);
    10.  
    11. }
    12.  
    13. bool MainWindow::keypres(QKeyEvent *keyevent)
    14. {
    15. if (keyevent->key()==Qt::Key_W)
    16. {
    17. qDebug() << "hi";
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    I just want to control keys and key events.
    Regards.

  2. #2
    Join Date
    Mar 2011
    Location
    Coimbatore,TamilNadu,India
    Posts
    382
    Thanks
    10
    Thanked 13 Times in 12 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

  3. #3
    Join Date
    Nov 2010
    Posts
    57
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Key Press Event Handling

    You need to set a focusPolicy

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Key Press Event Handling

    Your event handler signature is wrong.
    The documentation for QObject::installEventFilter() has an example exactly for key press events.
    Why not start there?
    http://doc.qt.nokia.com/latest/qobje...allEventFilter
    Last edited by high_flyer; 22nd March 2011 at 15:16. Reason: typo
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Key Press Event Handling

    Well then.
    @pan : i added focus policy
    and done somethings but thats have no effect.

    i made it with QDialog base class. But with QMainWindows its not working. I'm tired.
    I just open Qt Creator and create gui application, which is using QMainWindow base class.

    mainwindows.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QDebug>
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent), ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8. this->setWindowFlags(Qt::Dialog);
    9. this->setFocusPolicy(Qt::NoFocus);
    10. ui->textEdit->setFocusPolicy(Qt::StrongFocus);
    11. ui->textEdit->installEventFilter(this);
    12. }
    13.  
    14. bool MainWindow::catch_event(QObject *obj, QEvent *event)
    15. {
    16. if (obj == ui->textEdit && event->type()==QEvent::KeyPress)
    17. {
    18. QKeyEvent *keyevent = static_cast<QKeyEvent*>(event);
    19. qDebug() << "key event";
    20. return QObject::eventFilter(obj,event);
    21. }
    22. return QObject::eventFilter(obj,event);
    23. }
    To copy to clipboard, switch view to plain text mode 
    Problem is not solved but i'm tired about 2 days. I will do with QDialog class.

    Thanks all...

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Key Press Event Handling

    Problem is not solved but i'm tired about 2 days.
    I told you what the problem is!
    And I gave a link to almost exactly the code you need, you only need to copy paste and introduce the changes in the handler to do the filtering based on the rules you need it.
    So what is the problem?


    heh... you really should read your own signature and apply some though on it...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Key Press Event Handling

    Ok. Punch me
    Bro its really not working. You think, i make joke ?
    Try if you didnt believe ok.
    Thanks so.

    I'm missing something' s.. But i cant find it yet.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Key Press Event Handling


    The event filter filterObj receives events via its eventFilter() function.
    not 'catch_event'.

    all you had to do is COPY PASTE the example!!

    Qt Code:
    1. //change the class name to your
    2. bool KeyPressEater::eventFilter(QObject *obj, QEvent *event)
    3. {
    4. if (event->type() == QEvent::KeyPress) {
    5. //and here put your own logic!!
    6. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
    7. qDebug("Ate key press %d", keyEvent->key());
    8. return true;
    9. } else {
    10. // standard event processing
    11. return QObject::eventFilter(obj, event);
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Jan 2011
    Location
    Gordion
    Posts
    52
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Key Press Event Handling

    Well, show me a corner. I will cry at there...

    Qt Code:
    1. this->textEdit->installEventFilter(this);
    To copy to clipboard, switch view to plain text mode 
    I find where i missed up.
    My goal is make a terminal program like putty.
    Thanks.

Similar Threads

  1. Key Press Event and Shift key problem
    By Teefs in forum Qt Programming
    Replies: 7
    Last Post: 18th April 2011, 09:39
  2. Paint event function in key press event
    By soumya in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2010, 12:40
  3. Mouse press event detection
    By A.H.M. Mahfuzur Rahman in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2009, 13:08
  4. Key Press Event trouble
    By morraine in forum Newbie
    Replies: 6
    Last Post: 18th August 2008, 08:43
  5. Zooming on PushButton Press event
    By KosyakOFF in forum Qwt
    Replies: 5
    Last Post: 11th April 2008, 09:12

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.