PDA

View Full Version : QGraphicsItem and QKeyEvent problem



nearlyNERD
14th February 2010, 17:28
Hey,

I'm new here and I have a question.

I'm programming a little game and I'm using:

A main.cpp, where I create a QWidget. (MainWindow.cpp + MainWindow.h).

In the QWidget I create a QGraphicsView, a QGraphicsScene and a QGraphicsItem (mouse.cpp and mouse.h) and add the QGraphicsItem to the QGraphicsScene.

My problem is, that I want to move the QGraphicsItem with setPos(), when a Key is pressed, but I don't know, where I should implement the keyPressEvent as a function and how I should set the focuses, so my keyPress isn't ignored.
For example with setFocus(), clearFocus(), setFocusItem() (?)...

Is anybody able to help me? :confused:

nearlyNERD

Lykurg
14th February 2010, 17:36
since you don't have widgets that would react on a key press all is delivered to your main window. Just reimplement there your event handlers. Or you can install an event filter, that gets all events before they are delivered to the corresponding widgets.

psih128
14th February 2010, 17:47
You could also derive from QGraphicsScene and handle all the events you need at the global level. This way you wont miss any key presses, or other events. Then if you want specific items to handle key/mouse events, you should handle them per QGraphicsItem

nearlyNERD
14th February 2010, 18:09
My mainwidget.cpp (very shortened)


#include "mainwidget.h"

MainWidget::MainWidget(QWidget *parent)
: QWidget(parent)
{
this->setFocus(); // ???
QGraphicsScene scene;

scene.setSceneRect(0, 0, 470, 500);
scene.setItemIndexMethod(QGraphicsScene::NoIndex);


mouse = new Mouse;
mouse->neuePositionSetzen(50, 400);
scene.addItem(mouse);
scene.setFocusItem(mouse); // ???


QGraphicsView view(&scene);
view.setRenderHint(QPainter::Antialiasing);

view.resize(480, 600);
view.show();

QTimer timer;
QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
timer.start(35);


}

void MainWidget::keyPressEvent( QKeyEvent *event )
{
qDebug() << this->focusWidget()->accessibleName();
qDebug() << "Taste gedrückt!";

switch(event->key())
{
case Qt::Key_Right:
mouse->VerschiebeUm(2, 0);
qDebug() << "Rechts";
break;

case Qt::Key_Left:
mouse->VerschiebeUm(-2, 0);
qDebug() << "Left";
break;
}

}

and my mainwidget.h:




#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>

#include "mouse.h"

class MainWidget : public QWidget
{
public:
MainWidget(QWidget *parent = 0);

private:
Mouse *mouse;

protected:
virtual void keyPressEvent ( QKeyEvent *event );

};

#endif // MAINWIDGET_H

and my main.cpp:



#include "mainwidget.h"

#include <QApplication>
#include <QDebug>

int main(int argc, char **argv)
{
QApplication app(argc, argv);
MainWidget mainwidget;

mainwidget.setGeometry(100,100,500,355);
mainwidget.resize(480, 700);
mainwidget.setFocus(); // ????
qDebug() << app.focusWidget();
mainwidget.show();


return app.exec();
}


and there is also a mouse-class and a platform-class, but I think, they are not so important.

So, when i press a key, nothing happens. Why?

nearlyNERD
15th February 2010, 10:50
Does anyone have an idea how to fix that?

nearlyNERD
15th February 2010, 22:20
Can Somebody Help?

Lykurg
15th February 2010, 22:55
Since your code is confusing me just install an event filter on your application and see who gets the key event.
If you don't have any mouse interaction in your view I would set the noFocus policy to the view. if it is your only widget, then the mainwindow should receive the key event.