Results 1 to 1 of 1

Thread: Animate scale of QGraphicsObject upon selection with rubberband - not working

  1. #1
    Join Date
    Mar 2015
    Posts
    10
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Animate scale of QGraphicsObject upon selection with rubberband - not working

    Hi,

    I intend to create a custom scatterplot using the graphics view/scene/item framework.
    I am stuck with adding a feature that animates the scale of a custom ellipse item if selected.
    There should be two ways for selecting the item: by rubber band selection and/or by left mouse click.


    I have two questions:


    1. Why does the property animation not start if the:
    2. animate function is in the constructor?
    3. item is selected with the rubberband selection?


    Please feel free to comment on the general structure (choises of classes or animation approach or for ex.) of the programm as well.
    I am very new to Qt programming and still a bit overwhelmed by it.


    Here is my code:

    Qt Code:
    1. #ifndef MYELLIPSE_H
    2. #define MYELLIPSE_H
    3.  
    4. #include <QPainter>
    5. #include <QGraphicsObject>
    6. #include <QPropertyAnimation>
    7. #include <QGraphicsSceneMouseEvent>
    8.  
    9. #This class is a simple ellipse that should change it's color and start to animate it's scale once selected
    10. #and stop if "unselected". It should be selectable with either a mousclick on it, or if in the rectangle of a rubberband.
    11. #I chose the QGraphicsObject as the base class, because it works well with the
    12. #QPropertAnmiation class
    13. #I am aware of the QGraphicsItemAnimation, but it came to my attention that
    14. #it became obsolete and is not supprted anymore.
    15.  
    16. class myEllipse : public QGraphicsObject
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21.  
    22. myEllipse(QRectF red): rect(rec)
    23. {
    24. setFlag(ItemIsSelectable, true);
    25. setAcceptHoverEvents(true);
    26. _hover = false;
    27. _pressed = false;
    28.  
    29. #Does not work.
    30. #However if I put the animate function into the
    31. #mousePressedEvent it animates if I click with the mouse on it,
    32. #but not if it's selected with the rubber band.
    33. #What I don't understand is that since it changes it's color when selected with the
    34. #rubberband why it does not start the animation.
    35. if (this->isSelected()) {
    36. animate();
    37. }
    38. #constructor end
    39. }
    40. ~myEllipse(){}
    41.  
    42. QRectF boundingRect() const { return rect; }
    43. void paint(QPainter *painter,
    44. const QStyleOptionGraphicsItem *option,
    45. QWidget *widget) {
    46.  
    47. #This is working fine. Wheather I hover or click with the mouse over it
    48. #or put it in the rectangle of a rubber band, it alsways changes it's color.
    49. #Hence I assume that all three selection mode should be working.
    50. if (isSelected() || _hover)
    51. {
    52. painter->setPen(Qt::darkGray);
    53. } else {
    54. painter->setPen(Qt::black);
    55. }
    56. painter->drawEllipse(rect);
    57. }
    58.  
    59. protected:
    60.  
    61. #change color of pen if hover over
    62. virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *) { _hover = true; update(); }
    63.  
    64. #change color of pen back to black
    65. virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *) { _hover = false; update(); }
    66.  
    67. #select item and start animation
    68. virtual void mousePressEvent(QGraphicsSceneMouseEvent *){
    69. _pressed = true;
    70. if (ev->MouseButtonPress)
    71. {
    72. setSelected(true);
    73. #animate();
    74. }
    75.  
    76. }
    77. virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *){ _pressed = false; update(); }
    78.  
    79. private:
    80. QRectF rect;
    81. bool _hover;
    82. bool _pressed;
    83. void animate(){
    84. QPropertyAnimation *animation = new QPropertyAnimation(this, "scale");
    85. animation->setDuration(1000);
    86. animation->setStartValue(0.0);
    87. animation->setEndValue(1.0);
    88.  
    89. animation->setLoopCount(10);
    90. animation->start();
    91. }
    92. };
    93.  
    94. #endif // MYELLIPSE_H
    To copy to clipboard, switch view to plain text mode 

    And the mainWindow:

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QGraphicsScene>
    6. #include <QStandardItemModel>
    7. #include <QStringList>
    8. #include <QList>
    9. #include "myellipse.h"
    10.  
    11. namespace Ui {
    12. class MainWindow;
    13. }
    14.  
    15. #The ui contains a tableView and a graphicsview item
    16. class MainWindow : public QMainWindow
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. explicit MainWindow(QWidget *parent = 0): QMainWindow(parent),
    22. ui(new Ui::MainWindow)
    23. {
    24. ui->setupUi(this);
    25. setupModel();
    26. ui->tableView->setModel(model);
    27.  
    28. myOtherScene = new QGraphicsScene(this);
    29. ui->graphicsView_2->setDragMode(QGraphicsView::RubberBandDrag);
    30. ui->graphicsView_2->setScene(myOtherScene);
    31. setUpOtherScene();
    32. }
    33.  
    34. ~MainWindow();
    35.  
    36. private:
    37. Ui::MainWindow *ui;
    38. QStringList myList;
    39. QGraphicsScene *myOtherScene;
    40.  
    41. #Adds one custom graphics item from a standartitem model to the scene
    42. void setUpOtherScene(){
    43.  
    44. int row = 0;
    45. myEllipse *ellipse = new myEllipse(QRectF(-10*(model->data(model->index(row,1)).toDouble()),
    46. -10*(model->data(model->index(row,2)).toDouble()),
    47. 15, 15));
    48. myOtherScene->addItem(ellipse);
    49. }
    50. #Create a table with Four categories (A,B,C,D) in the first column and random ints from 1 to 10
    51. #in the second and third column.
    52. void setupModel(){
    53. myList << "A" << "B" << "C" << "D";
    54. model = new QStandardItemModel(4,3,this);
    55.  
    56. for (int row = 0; row < model->rowCount(QModelIndex()); ++row) {
    57. model->setData(model->index(row, 0, QModelIndex()),
    58. myList[row]);
    59. model->setData(model->index(row, 1, QModelIndex()),
    60. qrand()%10);
    61. model->setData(model->index(row, 2, QModelIndex()),
    62. qrand()%10);
    63. }
    64. }
    65.  
    66. };
    67.  
    68. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    I tried to get this working for days now, with some success but I need some help to make it over the finish line.
    Thank you many times.
    Last edited by vgrunert; 5th April 2015 at 15:56.

Similar Threads

  1. Replies: 1
    Last Post: 24th May 2012, 12:52
  2. How to get aspect ratio on selection with rubberBand?
    By suseway in forum Qt Programming
    Replies: 6
    Last Post: 25th October 2010, 09:29
  3. rubberband selection
    By franco.amato in forum Qt Programming
    Replies: 0
    Last Post: 22nd March 2010, 19:55
  4. Rubberband Item Selection
    By zgulser in forum Qt Programming
    Replies: 5
    Last Post: 23rd October 2009, 09:48
  5. QGraphicsView rubberband selection
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2007, 22:21

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.