Results 1 to 2 of 2

Thread: I need help with QPropertyAnimation

  1. #1
    Join Date
    Jun 2013
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default I need help with QPropertyAnimation

    Hello!
    I am working on a game, but I can't get the player to move when the WASD keys are pressed. I even tried making a new project and simply animating a square, but it wouldn't even work for me when I did that!
    NOTE: In the following code, I removed all my failed attempts at animating, so the code does run and there are no fatal errors.

    My code:

    dialog.h
    Qt Code:
    1. #include "player.h"
    2. #include "zombie.h"
    3.  
    4. #include <QDialog>
    5. #include <QGraphicsScene>
    6. #include <QPropertyAnimation>
    7.  
    8. namespace Ui {
    9. class Dialog;
    10. }
    11.  
    12. class Dialog : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Dialog(QWidget *parent = 0);
    18. ~Dialog();
    19. player *p;
    20. zombie *z;
    21. void keyPressEvent(QKeyEvent *event);
    22. private:
    23. Ui::Dialog *ui;
    24. };
    To copy to clipboard, switch view to plain text mode 

    player.h
    Qt Code:
    1. #include <QGraphicsItem>
    2. #include <QPropertyAnimation>
    3.  
    4. class player : public QGraphicsItem
    5. {
    6. public:
    7. player();
    8. QRectF boundingRect() const;
    9. void paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget);
    10. void moveUp();
    11. void moveLeft();
    12. void moveDown();
    13. void moveRight();
    14. private slots:
    15. };
    To copy to clipboard, switch view to plain text mode 

    dialog.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include "ui_dialog.h"
    3. #include "player.h"
    4. #include "zombie.h"
    5.  
    6. #include <QHBoxLayout>
    7. #include <QGraphicsView>
    8. #include <QGraphicsScene>
    9. #include <QSizePolicy>
    10. #include <QPropertyAnimation>
    11. #include <QKeyEvent>
    12. #include <iostream>
    13. #include <QPushButton>
    14.  
    15. using namespace std;
    16.  
    17. Dialog::Dialog(QWidget *parent) :
    18. QDialog(parent),
    19. ui(new Ui::Dialog)
    20. {
    21. ui->setupUi(this);
    22. this->setMinimumSize(1000, 700);
    23. this->setMaximumSize(size());
    24. this->setWindowTitle("Game");
    25.  
    26. QHBoxLayout *layout = new QHBoxLayout();
    27. this->setLayout(layout);
    28.  
    29. view->setRenderHints(QPainter::Antialiasing);
    30.  
    31. view->setScene(scene);
    32.  
    33. QBrush backgroundBrush(Qt::white);
    34. scene->setBackgroundBrush(backgroundBrush);
    35.  
    36. player *p = new player();
    37. scene->addItem(p);
    38.  
    39. zombie *z = new zombie();
    40. scene->addItem(z);
    41.  
    42. layout->addWidget(view);
    43. view->show();
    44. }
    45.  
    46. Dialog::~Dialog()
    47. {
    48. delete ui;
    49. qDebug("Dialog Deleted");
    50. }
    51.  
    52. void Dialog::keyPressEvent(QKeyEvent *event){
    53. switch(event->key()){
    54. case Qt::Key_W:
    55. qDebug("Key Pressed: W");
    56. p->moveUp();
    57. break;
    58. case Qt::Key_A:
    59. qDebug("Key Pressed: A");
    60. p->moveLeft();
    61. break;
    62. case Qt::Key_S:
    63. qDebug("Key Pressed: S");
    64. p->moveDown();
    65. break;
    66. case Qt::Key_D:
    67. qDebug("Key Pressed: D");
    68. p->moveRight();
    69. break;
    70. }
    71. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "dialog.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Dialog w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    player.cpp
    Qt Code:
    1. #include "player.h"
    2.  
    3. #include <QGraphicsItem>
    4. #include <QPainter>
    5. #include <QPropertyAnimation>
    6. #include <QObject>
    7.  
    8. player::player()
    9. {
    10. setFlag(QGraphicsItem::ItemIsMovable);
    11. qDebug("Player Created");
    12. }
    13.  
    14. QRectF player::boundingRect() const
    15. {
    16. return QRectF(0, 0, 25, 25);
    17. }
    18.  
    19. void player::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget)
    20. {
    21. QRectF rec = boundingRect();
    22.  
    23. painter->setBrush(Qt::red);
    24. painter->setPen(Qt::red);
    25.  
    26. painter->drawEllipse(rec);
    27. }
    28.  
    29. void player::moveUp(){
    30.  
    31. }
    32.  
    33. void player::moveLeft(){
    34.  
    35. }
    36.  
    37. void player::moveDown(){
    38.  
    39. }
    40.  
    41. void player::moveRight(){
    42.  
    43. }
    To copy to clipboard, switch view to plain text mode 

    I really wanted to put the respective animations in the functions moveUp(), moveLeft(), moveDown, moveRight() in the class player. Could you please help me by showing my how to do the animations in these locations.
    Also, while I'm here, is there anything I should change to my program while I am not to far into the game?

    Thanks
    -David

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: I need help with QPropertyAnimation

    I don't see how you can use QPropertyAnimation on your player class because it has no properties exposed. You would need to derive from QGraphicsObject which would give you a pos property to work with.

Similar Threads

  1. Replies: 21
    Last Post: 18th June 2013, 11:25
  2. QParallelAnimationGroup and QPropertyAnimation
    By nudels in forum Qt Programming
    Replies: 3
    Last Post: 26th January 2012, 12:17
  3. QPropertyAnimation doesnt do anything
    By superpacko in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2011, 17:04
  4. QPropertyAnimation dynamic end value is this possible?
    By GuusDavidson in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2011, 13:09
  5. Using QPropertyAnimation with QGraphicsPixmapItem
    By Luc4 in forum Qt Programming
    Replies: 8
    Last Post: 29th March 2010, 10:47

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.