My class:
class Jogador : public QWidget {
Q_OBJECT
public:
Jogador(QWidget* parent = NULL);
void draw();
void paintEvent(QPaintEvent* event);
void keyPressEvent(QKeyEvent* event);
private:
int x,y,w,h;
QVBoxLayout* layout;
};
My Role:
void Jogador::keyPressEvent(QKeyEvent* event) {
QWidget::keyPressEvent(event);
switch (event->key()) {
case Qt::Key_Up:
x+=20;
repaint();
break;
default:
break;
}
}
The question is: can I make the key event work in a class that inherits from QWidget
, or only QFrame
?
My main looks like this:
#include<QApplication>
#include<tabuleiro.h>
#include<jogador.h>
#include<QWidget>
int main(int argc, char* argv[]){
QApplication app(argc, argv);
QWidget window;
Tabuleiro t(&window);
Jogador j(&window);
window.show();
return app.exec();
return 0;
}