Results 1 to 7 of 7

Thread: QGraphicsItem and QKeyEvent problem

  1. #1
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGraphicsItem and QKeyEvent problem

    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?

    nearlyNERD

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QGraphicsItem and QKeyEvent problem

    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.

  3. #3
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QGraphicsItem and QKeyEvent problem

    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

  4. #4
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem and QKeyEvent problem

    My mainwidget.cpp (very shortened)

    Qt Code:
    1. #include "mainwidget.h"
    2.  
    3. MainWidget::MainWidget(QWidget *parent)
    4. : QWidget(parent)
    5. {
    6. this->setFocus(); // ???
    7.  
    8. scene.setSceneRect(0, 0, 470, 500);
    9. scene.setItemIndexMethod(QGraphicsScene::NoIndex);
    10.  
    11.  
    12. mouse = new Mouse;
    13. mouse->neuePositionSetzen(50, 400);
    14. scene.addItem(mouse);
    15. scene.setFocusItem(mouse); // ???
    16.  
    17.  
    18. QGraphicsView view(&scene);
    19. view.setRenderHint(QPainter::Antialiasing);
    20.  
    21. view.resize(480, 600);
    22. view.show();
    23.  
    24. QTimer timer;
    25. QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance()));
    26. timer.start(35);
    27.  
    28.  
    29. }
    30.  
    31. void MainWidget::keyPressEvent( QKeyEvent *event )
    32. {
    33. qDebug() << this->focusWidget()->accessibleName();
    34. qDebug() << "Taste gedrückt!";
    35.  
    36. switch(event->key())
    37. {
    38. case Qt::Key_Right:
    39. mouse->VerschiebeUm(2, 0);
    40. qDebug() << "Rechts";
    41. break;
    42.  
    43. case Qt::Key_Left:
    44. mouse->VerschiebeUm(-2, 0);
    45. qDebug() << "Left";
    46. break;
    47. }
    48.  
    49. }
    To copy to clipboard, switch view to plain text mode 

    and my mainwidget.h:

    Qt Code:
    1. #ifndef MAINWIDGET_H
    2. #define MAINWIDGET_H
    3.  
    4. #include <QWidget>
    5.  
    6. #include "mouse.h"
    7.  
    8. class MainWidget : public QWidget
    9. {
    10. public:
    11. MainWidget(QWidget *parent = 0);
    12.  
    13. private:
    14. Mouse *mouse;
    15.  
    16. protected:
    17. virtual void keyPressEvent ( QKeyEvent *event );
    18.  
    19. };
    20.  
    21. #endif // MAINWIDGET_H
    To copy to clipboard, switch view to plain text mode 

    and my main.cpp:

    Qt Code:
    1. #include "mainwidget.h"
    2.  
    3. #include <QApplication>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. QApplication app(argc, argv);
    9. MainWidget mainwidget;
    10.  
    11. mainwidget.setGeometry(100,100,500,355);
    12. mainwidget.resize(480, 700);
    13. mainwidget.setFocus(); // ????
    14. qDebug() << app.focusWidget();
    15. mainwidget.show();
    16.  
    17.  
    18. return app.exec();
    19. }
    To copy to clipboard, switch view to plain text mode 

    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?

  5. #5
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem and QKeyEvent problem

    Does anyone have an idea how to fix that?

  6. #6
    Join Date
    Feb 2010
    Posts
    13
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsItem and QKeyEvent problem

    Can Somebody Help?

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QGraphicsItem and QKeyEvent problem

    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.

Similar Threads

  1. QKeyEvent problem with different keyboard layout
    By minimoog in forum Qt Programming
    Replies: 3
    Last Post: 12th February 2010, 06:45
  2. Something about the QKeyEvent in Qt3
    By cspp in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2009, 02:38
  3. QKeyEvent Problem
    By cutie.monkey in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2008, 23:36
  4. QKeyEvent
    By peace_comp in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2008, 13:13
  5. QKeyEvent
    By sonuani in forum Newbie
    Replies: 8
    Last Post: 25th February 2008, 07:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.