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. keyPressEvent(QKeyEvent* event) not triggering
Qt 6.11 is out! See what's new in the release blog

keyPressEvent(QKeyEvent* event) not triggering

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 3.2k Views 2 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.
  • U Offline
    U Offline
    U7Development
    wrote on last edited by U7Development
    #1

    Hi!.
    keyPressEvent works as espected if it is inside a QDialog form class. But i wanted to create a separated custom class debug.hpp to handle it..

    #ifndef DEBUG_HPP
    #define DEBUG_HPP
    
    #include <QKeyEvent>
    #include <QLabel>
    #include <QMessageBox>
    
    struct debug {
    private:
    	static QWidget* obj;
    
    	void keyPressEvent(QKeyEvent* event){
    		const QRect r = obj->geometry();
    
    		switch (event->key()){
    		case Qt::Key_W:
    			obj->move(r.x(), r.y() - 1);
    			break;
    		case Qt::Key_Y:	
    			obj->move(r.x(), r.y() - 24);
    			break;
    		case Qt::Key_S:
    			obj->move(r.x(), r.y() + 1);
    			break;
    		case Qt::Key_H:
    			obj->move(r.x(), r.y() + 24);
    			break;
    		case Qt::Key_A:
    			obj->move(r.x() - 1, r.y());
    			break;
    		case Qt::Key_G:
    			obj->move(r.x() - 24, r.y());
    			break;
    		case Qt::Key_D:
    			obj->move(r.x() + 1, r.y());
    			break;
    		case Qt::Key_J:
    			obj->move(r.x() + 24, r.y());
    			break;
    		case Qt::Key_Return:
    			QMessageBox::information(0, "DEBUG", QString::number(r.x()) + " : " + QString::number(r.y()));
    			break;
    		}
    	}
    
    public:
    	debug() = delete;
            //setup widget that needs to be arranged	
    	inline static void DHANDLE(QWidget* _o){
    		if (!_o) return;
    		obj = _o;
    	}
    };
    
    QWidget* debug::obj = nullptr;
    #endif /DEBUG_HPP
    

    Basically the only callable function is DHANDLE where i set a widget to be controlled with A,W,S,D keys.
    From a form class cpp i call:

    #include "debug.hpp"
    void myclass::foo(){
         QPushButton myButton = new QPushButton(this);
         debug::DHANDLE(myButton);   
    }
    

    This should allow me to control myButton position (geometry) using WASD keys, but nothing happens.. i know DHANDLE is being called (i have qDebugged()), but keyPressEvent() is not...

    What could it be?
    thanks.

    Edit: I forgot to say i'm not using Qt Creator but VIM to write my code so im blindly working, thats why i want to implement a position debugging way to know where to locate my widgets.

    1 Reply Last reply
    0
    • jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      The class needs to derive from QWindow or QWidget for the event loop to know that calling keyPressEvent is an option, and then the instance needs to be in the keyboard focus chain.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      4
      • U Offline
        U Offline
        U7Development
        wrote on last edited by
        #3

        i'll inherits from QWindow or QWidget..
        what do you mean by keyboard focus chain?

        Thanks

        jsulmJ artwawA 2 Replies Last reply
        0
        • U U7Development

          i'll inherits from QWindow or QWidget..
          what do you mean by keyboard focus chain?

          Thanks

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @U7Development said in keyPressEvent(QKeyEvent* event) not triggering:

          what do you mean by keyboard focus chain?

          The widget needs to have the focus to get key press events

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • U U7Development

            i'll inherits from QWindow or QWidget..
            what do you mean by keyboard focus chain?

            Thanks

            artwawA Offline
            artwawA Offline
            artwaw
            wrote on last edited by
            #5

            @U7Development you can (assuming you know when the widget needs to listen to the key events) to force focus by using grabKeyboard() - just remember to unset it by calling releaseKeyboard() afterwards.

            For more information please re-read.

            Kind Regards,
            Artur

            1 Reply Last reply
            1
            • U Offline
              U Offline
              U7Development
              wrote on last edited by
              #6

              alright thanks I will give a try and comment later..

              1 Reply Last reply
              0
              • U Offline
                U Offline
                U7Development
                wrote on last edited by
                #7

                Update, since I need to inherits from QWindow or QWidget that does not had sense to me for the purpose of the debug, so I move the function to an abstract class derived from QDialog, and I got working!!.. Now I have another related to Key issue but I will do another thread for it..

                Thanks so much!

                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