Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Help with keypress event
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Help with keypress event

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 7.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Code_ReaQtor
    wrote on last edited by
    #2

    Pls check this "thread":http://qt-project.org/forums/viewthread/1243

    Please visit my open-source projects at https://github.com/Code-ReaQtor.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #3

      You just need to change

      line 17: of mainwindow.h to

      @void keyPressEvent(QKeyEvent *event);@

      and same for your .cpp file

      check "keyPressEvent":http://qt-project.org/doc/qt-5.0/qtwidgets/qwidget.html#keyPressEvent

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Code_ReaQtor
        wrote on last edited by
        #4

        [quote author="Sam" date="1358924236"]You just need to change

        line 17: of mainwindow.h to

        @void keyPressEvent(QKeyEvent *event);@

        and same for your .cpp file

        check "keyPressEvent":http://qt-project.org/doc/qt-5.0/qtwidgets/qwidget.html#keyPressEvent[/quote]

        I think it doesn't matter what you use. *keyevent or *event is still the same.

        EDIT: sorry Sam, i rechecked and found out that it wasn't the right function that was used.

        Please visit my open-source projects at https://github.com/Code-ReaQtor.

        1 Reply Last reply
        0
        • H Offline
          H Offline
          holygirl
          wrote on last edited by
          #5

          I did read that link before I posted here. I wasn't able to relate it to my question. I feel I have the right code but I'm missing something. I don't know what that is.

          1 Reply Last reply
          0
          • H Offline
            H Offline
            holygirl
            wrote on last edited by
            #6

            And Sam, I'll try what you suggested and get back to you. Thanks!

            1 Reply Last reply
            0
            • H Offline
              H Offline
              holygirl
              wrote on last edited by
              #7

              I tried using *event in both my .cpp and .h file. But nothing is happening. Is it something to do with setFocusPolicy?
              I tried reading "this":http://www.qtcentre.org/threads/45159-keyPressEvent-not-being-called but I still couldn't get my program to work.
              Please help!

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #8

                I tried a sample code the same that you have :

                @#include <QMainWindow>
                #include <QKeyEvent>

                namespace Ui {
                class MainWindow;
                }

                class MainWindow : public QMainWindow
                {
                Q_OBJECT

                public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();

                private:
                Ui::MainWindow *ui;

                protected:
                void keyPressEvent(QKeyEvent *event);
                };@

                .cpp

                @MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {
                ui->setupUi(this);
                }

                MainWindow::~MainWindow()
                {
                delete ui;
                }

                void MainWindow::keyPressEvent(QKeyEvent *event)
                {

                if (event->key() == Qt::Key_Escape)
                    this->close();
                

                }@

                works as expected as there is nothing inside the mainwindow, other option can be to use eventFilter and there also you can check for keypressevent.

                something like

                @MainWindow::MainWindow(QWidget *parent) :
                QMainWindow(parent),
                ui(new Ui::MainWindow)
                {
                ui->setupUi(this);
                installEventFilter(this);
                }

                MainWindow::~MainWindow()
                {
                delete ui;
                }

                bool MainWindow::eventFilter(QObject *target, QEvent *event)
                {
                if (event->type() == QEvent::KeyPress)
                {
                QKeyEvent keyEvent = static_cast<QKeyEvent>(event);

                    if (keyEvent->key() == Qt::Key_Escape)
                    {
                        this->close();
                        return QMainWindow::eventFilter(target,event);
                    }
                }
                return QMainWindow::eventFilter(target,event);
                

                }@

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  holygirl
                  wrote on last edited by
                  #9

                  Finally! It worked. I tried using installEventFilter earlier but I was clear on how to use it so I went back to the regular keypress event which didn't work.

                  Thank you so much for your time and help.

                  If it's not too much trouble, could you please explain why eventfilter worked but not my earlier code? I'm new to Qt and the documentation doesn't always help me understand things.

                  Thanks again!

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Sam
                    wrote on last edited by
                    #10

                    [quote author="holygirl" date="1358929707"]I tried using *event in both my .cpp and .h file. But nothing is happening. Is it something to do with setFocusPolicy?
                    I tried reading "this":http://www.qtcentre.org/threads/45159-keyPressEvent-not-being-called but I still couldn't get my program to work.
                    Please help![/quote]

                    I was not talking about changing *e to *event , Its about changing keypress to keyPressEvent.

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      holygirl
                      wrote on last edited by
                      #11

                      Oh! I'm so sorry. I don't know how I missed seeing that. Thank you so much for correcting me. And many more thanks for all your help :)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved