Results 1 to 9 of 9

Thread: QGraphicsObject color change animation

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

    Default QGraphicsObject color change animation

    I have created QGraphicsObject which has multiple rectangles attached to each other. All these rects has default color as orange. This is working correctly. Now I want to change color of one particular rect after certain condition is met. I am not sure how animation will take place to change color of one rect.

    I am attaching my implemention for your reference.


    Thanks

    Manish
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsObject color change animation

    What's exactly the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    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

    suppose using attached object, I made an object like this.

    Qt Code:
    1. arrayitem *ai = new arrayitem(QStringList() << "55" << "77",QFont("Calibri",20)); // This will create 2 boxes containing text 55 and 77 having background orange
    2. for(int i=0; i<20; i++) {
    3. if (i==10) {
    4. // want to change background color of 55 box to green. what changes are required in attached class.
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 29th August 2011 at 09:10. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsObject color change animation

    You need to provide a method in the item class for setting the color (and scheduling an update) and the color needs to be taken into consideration when painting the item.

    However you will have to get rid of the for loop, it doesn't make sense.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #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

  6. #6
    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

    Any suggession on this

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QGraphicsObject color change animation

    Can you provide a compilable example reproducing the problem? The one you posted is incomplete.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    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

    Problem is resolved.

    Thanks you.


    Manish

  9. #9
    Join Date
    Aug 2011
    Location
    Pune, India
    Posts
    18
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QGraphicsObject color change animation

    Hi,

    Could you please provide us the complete working code of this.

    Thanks,
    Akash

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