Results 1 to 2 of 2

Thread: Turn items and progress

  1. #1
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Turn items and progress

    Hello everyone, I have created (with QtCreator 2.5.2 and Qt 4.8.2) a program that displays a rectangle in my QGraphicsScene.
    This rectangle walking along the y axis of the scene. I wish to advance over the rectangle can also rotate around its center of gravity (always maintaining the path along the y axis). For the animation use QGraphicsItem::advance(int phase) together with a timer defined in the constructor of my item (derived from the class QGraphicsItem). How should I do?
    This is my code for the item:
    Qt Code:
    1. #include "robot.h"
    2. #include <QDebug>
    3.  
    4. Robot::Robot() : angolo(10), velocita(5), posizione(0, 0), flagCollisione(false)
    5. {
    6. // posizione iniziale rispetto angolo in alto a sinsitra della scena
    7. int startX = 200;
    8. int startY = 200;
    9.  
    10. // posiziono il baricentro del robot nel centro della scena
    11. setPos(mapToParent(startX, startY));
    12.  
    13. // rendo possibile spostare il robot con il mouse
    14. this->setFlags(QGraphicsItem::ItemIsMovable);
    15. }
    16.  
    17. // definisci le dimensioni perimetrali del robot
    18. QRectF Robot::boundingRect() const
    19. {
    20. // origine assi robot angolo in alto a sinistra
    21. //return QRect(0, 0, 20, 30);
    22.  
    23. // origine assi robot nel baricentro del robot
    24. return QRect(-10, -15, 20, 30);
    25. }
    26.  
    27. // ridisegna robot ogni volta che si fa setPos()
    28. void Robot::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    29. {
    30. QRectF corpo = boundingRect();
    31.  
    32. // definisci colore pennello area robot: grigio
    33. QBrush Pennello(Qt::gray);
    34.  
    35. // individuazione base collisione
    36. if(scene()->collidingItems(this).isEmpty())
    37. {
    38. // nessuna collisione: usa colore area verde item
    39. Pennello.setColor(Qt::green);
    40. }
    41. else
    42. {
    43. // collisione!!!: usa colore area rossa item
    44. Pennello.setColor(Qt::red);
    45. flagCollisione = !flagCollisione;
    46.  
    47. // individua il punto di collisione
    48. vaiCollisione();
    49. }
    50.  
    51. // colora area robot
    52. painter->fillRect(corpo, Pennello);
    53.  
    54. // ridisegna robot
    55. painter->drawRect(corpo);
    56. }
    57.  
    58. void Robot::advance(int phase)
    59. {
    60. if(!phase) return;
    61.  
    62. // leggi la posizione attuale
    63. posizione = this->pos();
    64.  
    65. if(flagCollisione == false)
    66. setPos(mapToParent(0, -(velocita)));
    67. else if(flagCollisione == true)
    68. setPos(mapToParent(0, velocita));
    69. }
    70.  
    71. void Robot::vaiCollisione()
    72. {
    73. // leggi una nuova posizione
    74. posizione = this->pos();
    75.  
    76. // vedere se la nuova posizione è nel perimetro
    77. if(!(scene()->sceneRect().contains(posizione)))
    78. {
    79. // siamo fuori dell'area scena
    80.  
    81. // muovilo indietro nel perimetro
    82. setPos(mapToParent(200, velocita));
    83. posizione = this->pos();
    84. flagCollisione = false;
    85. }
    86. else
    87. {
    88. // siamo all'interno dell'area scena
    89. // imposta la nuova posizione
    90. setPos(posizione);
    91. }
    92. }
    To copy to clipboard, switch view to plain text mode 
    while the MainWindow is:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "robot.h"
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent),
    7. ui(new Ui::MainWindow)
    8. {
    9. ui->setupUi(this);
    10.  
    11. pScena = new QGraphicsScene(this);
    12. ui->graphicsView->setScene(pScena);
    13.  
    14. ui->graphicsView->setRenderHint(QPainter::Antialiasing);
    15. pScena->setSceneRect(0, 0, 400, 400);
    16.  
    17. QPen miaPenna = QPen(Qt::red);
    18.  
    19. // crea il contorno scena
    20. QLineF LineaAlto(pScena->sceneRect().topLeft(), pScena->sceneRect().topRight());
    21. QLineF LineaSinistra(pScena->sceneRect().topLeft(), pScena->sceneRect().bottomLeft());
    22. QLineF LineaDestra(pScena->sceneRect().topRight(), pScena->sceneRect().bottomRight());
    23. QLineF LineaBasso(pScena->sceneRect().bottomLeft(), pScena->sceneRect().bottomRight());
    24.  
    25. pScena->addLine(LineaAlto, miaPenna);
    26. pScena->addLine(LineaSinistra, miaPenna);
    27. pScena->addLine(LineaDestra, miaPenna);
    28. pScena->addLine(LineaBasso, miaPenna);
    29.  
    30. int contaRobot = 1;
    31.  
    32. for(int i = 0; i < contaRobot; i++)
    33. {
    34. Robot *pRobot = new Robot();
    35. pScena->addItem(pRobot);
    36. }
    37.  
    38. pTimer = new QTimer(this);
    39. connect(pTimer, SIGNAL(timeout()), pScena, SLOT(advance()));
    40. pTimer->start(100);
    41. }
    42.  
    43. MainWindow::~MainWindow()
    44. {
    45. delete ui;
    46. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2010
    Location
    Italia
    Posts
    149
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Turn items and progress

    Resolved:
    Qt Code:
    1. void Robot::advance(int phase)
    2. {
    3. if(!phase) return;
    4.  
    5. angolo += 10;
    6.  
    7. if(flagCollisione == false)
    8. {
    9. avanzamento += 5;
    10. }
    11. else if(flagCollisione == true)
    12. {
    13. avanzamento -= 5;
    14. }
    15. // rototraslazione
    16. setTransform(QTransform().translate(0, -(avanzamento)).rotate(angolo));
    17. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to turn on optimization in qt??
    By tonylin0826 in forum Newbie
    Replies: 5
    Last Post: 7th September 2012, 11:12
  2. Custom progress bar background for list items
    By themagician in forum Qt Programming
    Replies: 1
    Last Post: 26th February 2012, 22:07
  3. How to turn off PC?
    By wydesenej in forum Qt Programming
    Replies: 1
    Last Post: 20th January 2009, 17:56
  4. Replies: 4
    Last Post: 11th March 2008, 11:44
  5. Turn off computer
    By swiety in forum KDE Forum
    Replies: 1
    Last Post: 5th January 2008, 15:40

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.