PDA

View Full Version : KeyPressEvent()



Knogger
6th November 2017, 00:47
Hello,

I'm pretty new to Qt and wanted to make a small puzzle game.

So I tried to use the KeyPressEvent() function to receive the keyboard input,
but if I try to use some memberfunctions or variables of my game class inside the KeyPressEvent() function it doesn't work.
It's written in Visual Studio.
I hope that someone can tell me where I messed up.

Here is a section of my programmtext:

#include "game.h"
#include "ui_game.h"

game::game(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);

scene = new QGraphicsScene(this);
ui.graphicsView->setScene(scene);

fDraw(boxPos);
}

int game::fPosCalc(int counter)
{...}

void game::fDraw(int field[3][3])
{...}

void QWidget::keyPressEvent(QKeyEvent *w)
{
game.boxPos[3][3] = 1;
game.fDraw(boxPos)
}

d_stranz
6th November 2017, 01:54
void QWidget::keyPressEvent(QKeyEvent *w)
{
game.boxPos[3][3] = 1;
game.fDraw(boxPos)
}

This is one of the things that is "messed up". This keyPressEvent() method should be a protected method of the "game" class. You should read about "overloading" in C++; you should know that you don't override a virtual function of a base class by simply redefining it. You should also know that you can't call a member function of any class using the "dot" notation: game.fDraw() is nonsense - it won't even compile.

Knogger
6th November 2017, 17:02
I will look that up,

Thank you very much for your advise.