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. Keypress events
Qt 6.11 is out! See what's new in the release blog

Keypress events

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 3.3k 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.
  • Y Offline
    Y Offline
    yhu420
    wrote on last edited by
    #1

    Hello everyone,
    Today I satrted wondering how the keypress events work in Qt, so I started to make a small app to check that out. Unfortunately I can't manage to get it working :( . All I get is the button in the middle, but its never moves..
    Here's the code:
    @#include "Mainwin.h"
    #include <QKeyEvent>
    #include <QKeySequence>
    #include <QPoint>
    #include <QDebug>

    Mainwin::Mainwin() : QWidget()
    {
    but = new QPushButton(this);
    but->setText("O");
    but->setVisible(true);
    this->setFixedSize(600,600);
    but->move(300,300);

    waitforkp();
    

    }

    void Mainwin::waitforkp()
    {
    QKeyEvent *event = new QKeyEvent(QEvent::KeyPress,0,Qt::NoModifier,"",false,4);
    if (event->key() == Qt::Key_Down){
    but->move(but->pos().x(),but->pos().y()-1);
    qDebug() << "in down";
    }

    if (event->key() == Qt::Key_Left){
    but->move(but->pos().x()-1,but->pos().y());
    

    }

    if (event->key() == Qt::Key_Up){
    
    but->move(but->pos().x(),but->pos().y()+1);
    

    }

    if (event->key() == Qt::Key_Right){
    but->move(but->pos().x()+1,but->pos().y());
    

    }
    qDebug() << but->pos();
    }
    @

    Also, I guess it's not the "conventional way" to do it.
    Thanks for every answer I get ;)

    EDIT: I never see the "in down" debug I placed in waitforkp()

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Your need to override keyPressEvent(QKeyEvent * e).
      @void keyPressEvent(QKeyEvent *)
      {
      }@
      Then move all your code which is in waitforkp() into keyPressEvent(QKeyEvent * e)

      157

      1 Reply Last reply
      0
      • JeroentjehomeJ Offline
        JeroentjehomeJ Offline
        Jeroentjehome
        wrote on last edited by
        #3

        Hi,
        Small fix for above code:
        @
        void Mainwin::keyPressEvent(QKeyEvent *)
        {
        .... // code here
        }
        @
        is the correct syntax. For all Q_Object derived classes your are able to overwrite the events, such as timerEvent, changeEvent etc etc. For every event there is a eventhandler.

        Greetz, Jeroen

        1 Reply Last reply
        0
        • Y Offline
          Y Offline
          yhu420
          wrote on last edited by
          #4

          This still doesn't work :(
          Here's my updated code:
          @#include "Mainwin.h"
          #include <QKeyEvent>
          #include <QKeySequence>
          #include <QPoint>
          #include <QDebug>

          Mainwin::Mainwin() : QWidget()
          {
          but = new QPushButton(this);
          but->setText("O");
          but->setVisible(true);
          this->setFixedSize(600,600);
          but->move(300,300);

          QKeyEvent *event = new QKeyEvent(QEvent::KeyPress,0,Qt::NoModifier,"",false,4);
          
          keyPressEvent(event);
          

          }

          void Mainwin::keyPressEvent(QKeyEvent *event)
          {

          if (event->key() == Qt::Key_Down){
          but->move(but->pos().x(),but->pos().y()-1);
          qDebug() << "in down";
          

          }

          if (event->key() == Qt::Key_Left){
          but->move(but->pos().x()-1,but->pos().y());
          

          }

          if (event->key() == Qt::Key_Up){
          
          but->move(but->pos().x(),but->pos().y()+1);
          

          }

          if (event->key() == Qt::Key_Right){
          but->move(but->pos().x()+1,but->pos().y());
          

          }
          qDebug() << but->pos();
          }

          @
          And I still can't see the "in down" debug info..

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            don't do that (in your constructor):
            @
            QKeyEvent *event = new QKeyEvent(QEvent::KeyPress,0,Qt::NoModifier,"",false,4);

            keyPressEvent(event);
            @

            rather use QApplication::postEvent().
            Or better use your keyboard ;)

            And for clarification why you don't see the debug output. You create your key event with a key of value 0 instead of Qt::Key_Down.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            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