Why can’t I capture Keyevent in this program ? Problems are in the main?

Asked

Viewed 59 times

0

#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;
}

Classes:

#include<tabuleiro.h>
#include<QPainter>
#include<iostream>

using namespace std;

Tabuleiro::Tabuleiro(QWidget* parent)
: QWidget(parent){


layout = new QVBoxLayout();


gerarTabuleiro(1);
repaint();
setFixedSize(960,560);
setLayout(layout);

show();

}

void Tabuleiro::gerarTabuleiro(int modo){

x=0;
y=0;
w=40;
h=40;

switch(modo){

case 1 :

 for(int x = 0;  x<14 ; x++){
        for(int y = 0 ; y<12 ; y++){
            tabuleiro[x][y] ='1';
        }
    }

 for(int x = 0;  x<14 ; x++){
        for(int y = 12 ; y<24 ; y++){
            tabuleiro[x][y] ='0';
        }
    }

 break;


}




}

void Tabuleiro::draw(){
repaint();
}

void Tabuleiro::paintEvent(QPaintEvent *event){
QWidget::paintEvent(event);
QPainter painter(this);
painter.setPen(Qt::gray);

for(int c = 0 ; c<14 ; c++){


    for(int l = 0 ; l<24 ; l++){

        if(tabuleiro[c][l]=='1'){
            painter.setBrush(Qt::black);

        }
        else{
         painter.setBrush(Qt::white);
        }



        painter.drawRect(x,y,w,h);

        x+=40;



    }
    x=0;
    y+=40;
 }
y=0;

}

My problem that I’m not getting captures the keyboard Event ode.

  • 1

    When you say you "can’t" is because you’re getting an error or because you don’t know how to do it? Have you ever tried to reimplement the method keyPressEvent in your class Tabuleiro?

  • I have reimplemented, but when running the application does not call the method.

  • Implemented where, how? Not in the code you shared.

1 answer

0

You need to include the Qkeyevent class and reimplement the function void keyReleaseEvent(QKeyEvent* event), for your keyboard events to be recognized.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.