Results 1 to 9 of 9

Thread: QGraphicsObject color change animation

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #5
    Join Date
    Jun 2010
    Posts
    97
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsObject color change animation

    Pl. see below code code.
    after using qpropertyanimation updation of qgraphicsobject is not happening properly.



    Qt Code:
    1. #ifndef MYARRAY_H
    2. #define MYARRAY_H
    3.  
    4. #include <QtGui>
    5.  
    6. class myarray : public QGraphicsObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit myarray(QStringList nlist, QGraphicsItem *parent = 0);
    11. QRectF boundingRect() const;
    12. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    13.  
    14. signals:
    15.  
    16. public slots:
    17.  
    18. private:
    19. QStringList vlist;
    20. QFont appFont;
    21. int x, y, wid, ht;
    22. int cntr;
    23. int totwid;
    24. bool isNonArray;
    25.  
    26. };
    27.  
    28. #endif // MYARRAY_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "myarray.h"
    2.  
    3. myarray::myarray(QStringList nlist, QGraphicsItem *parent) :
    4. QGraphicsObject(parent)
    5. {
    6. vlist = nlist;
    7. x=0;
    8. y=0;
    9. wid=0;
    10. ht=0;
    11. cntr=0;
    12. QFont font("Calibri",14);
    13. QFontMetrics fm(font);
    14. foreach(QString s, vlist){
    15. wid += fm.width(s);
    16. qDebug() << fm.width(s);
    17. }
    18. totwid=wid;
    19. ht = fm.height();
    20. appFont = font;
    21. }
    22.  
    23. QRectF myarray::boundingRect() const
    24. {
    25. return QRectF(0,0,wid+10,ht+10);
    26. }
    27.  
    28. void myarray::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    29. {
    30. Q_UNUSED(option);
    31. Q_UNUSED(widget);
    32. wid=0;
    33. cntr=0;
    34. if (cntr++==0){
    35. x=0;
    36. y=0;
    37. }
    38. qDebug() << cntr;
    39. painter->save();
    40. painter->setRenderHint(QPainter::Antialiasing, true);
    41. painter->setPen(Qt::red);
    42. painter->setBrush(Qt::yellow);
    43. foreach(QString s, vlist){
    44. QFontMetrics fm(appFont);
    45. wid = fm.width(s);
    46. painter->drawRect(x,y,wid+10,ht+10);
    47. painter->setFont(appFont);
    48. painter->drawText(QRect(x+5,y+5,wid,ht),Qt::AlignHCenter|Qt::AlignVCenter,s);
    49. x += wid+10;
    50. }
    51. painter->restore();
    52. update();
    53. }
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #ifndef TESTGRAPHICS_H
    2. #define TESTGRAPHICS_H
    3.  
    4. #include <QtGui>
    5. #include "myarray.h"
    6.  
    7. namespace Ui {
    8. class testgraphics;
    9. }
    10.  
    11. class testgraphics : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit testgraphics(QWidget *parent = 0);
    17. ~testgraphics();
    18.  
    19. private slots:
    20. void fin();
    21.  
    22. private:
    23. Ui::testgraphics *ui;
    24. myarray *m;
    25. };
    26.  
    27. #endif // TESTGRAPHICS_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "testgraphics.h"
    2. #include "ui_testgraphics.h"
    3.  
    4. testgraphics::testgraphics(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::testgraphics)
    7. {
    8. ui->setupUi(this);
    9. ui->graphicsView->setScene(scene);
    10. scene->setSceneRect(QRect(0,0,500,500));
    11. rect->setRect(0,0,350,350);
    12. scene->addItem(rect);
    13. text->setPlainText("Hello");
    14. text->setPos(100,100);
    15. text->setParentItem(rect);
    16.  
    17. m = new myarray(QStringList() << "152" << "116");
    18. m->setPos(150,150);
    19. m->setParentItem(rect);
    20. m->setOpacity(0.2);
    21. QPropertyAnimation *anim = new QPropertyAnimation(m,"opacity");
    22. anim->setDuration(5000);
    23. anim->setEndValue(1.0);
    24. connect(anim,SIGNAL(finished()),this,SLOT(fin()));
    25. anim->start(QPropertyAnimation::DeleteWhenStopped);
    26. }
    27.  
    28. testgraphics::~testgraphics()
    29. {
    30. delete ui;
    31. }
    32.  
    33. void testgraphics::fin()
    34. {
    35. m->update();
    36. }
    To copy to clipboard, switch view to plain text mode 





    Regards
    Manish
    Last edited by wysota; 29th August 2011 at 16:48. Reason: missing [code] tags

Similar Threads

  1. How to make an QGraphicsObject animation?
    By luisvaldes88 in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2011, 18:01
  2. Change in color map?
    By bigjoeystud in forum Qwt
    Replies: 4
    Last Post: 8th September 2010, 20:00
  3. Replies: 3
    Last Post: 22nd January 2010, 16:46
  4. how to change backgroup color, button color and shape?
    By lzha022 in forum Qt Programming
    Replies: 10
    Last Post: 16th June 2008, 22:25
  5. How to change color of a QPushButton?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 5th March 2008, 13:22

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.