Please nominate your Qt Champions for 2021! https://forum.qt.io/topic/132134/looking-for-the-2021-qt-champions

keyPressEvent(QKeyEvent* event) not triggering



  • 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.



  • 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.



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

    Thanks


  • Lifetime Qt Champion

    @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



  • @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.



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



  • 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!


Log in to reply