Results 1 to 16 of 16

Thread: Filling a QGraphicsItem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2008
    Posts
    52
    Qt products
    Qt4
    Thanks
    3

    Default Re: Filling a QGraphicsItem

    As you know that in DiagramScene example I have a MainWindow in that DiagramScene on that with mousce click I am placing DiagramItem which is a QGraphicsPolygonItem in that class there is no paint() only setPolygon is there.
    So do i have to implement Paint() and if i write my fillItem() in MainWindow then i have to call Item's paint in a loop to get the effact i want.
    can u just show me sample code.
    Thank u,
    regards.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 334 Times in 317 Posts

    Default Re: Filling a QGraphicsItem

    QGraphicsPolygonItem is inherited from QGraphicsItem and hence you can always override the paint function.

    You will need to inherit from QGraphicsPolygonItem and reimplement paint function.
    In that paint function u can call the base class paint function and then fill the desired area in your class paint function.

  3. #3
    Join Date
    Dec 2008
    Posts
    52
    Qt products
    Qt4
    Thanks
    3

    Default Re: Filling a QGraphicsItem

    If I do so then the Paint Function of the Item is called only once when it is created. Then how can I show the Filling effect on the scene. I fill the Item in a loop with some delay in each filling untill its full.

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

    Default Re: Filling a QGraphicsItem

    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
    Dec 2008
    Posts
    52
    Qt products
    Qt4
    Thanks
    3

    Default Re: Filling a QGraphicsItem

    Thanx for reply, but I am not able to form the paint logic, plz help to to fill item in a loop with 500ms wait.

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

    Default Re: Filling a QGraphicsItem

    If you're not able to reimplement painting of the item then you can't fill the item in a loop with 500ms wait. Simple as that.
    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.


  7. #7
    Join Date
    Dec 2008
    Posts
    52
    Qt products
    Qt4
    Thanks
    3

    Default Re: Filling a QGraphicsItem

    I have reimplemented paint and I have drawn my Item in Paint and i have fill it in one go, which i dont want i want to fill it from mainwindow where a user click fill button then the Item should get filled 10%,20%,30%........100% with 500ms wait this I want to keep in a loop so that i can get filling effect.

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

    Default Re: Filling a QGraphicsItem

    Look, you have been given all the answers you need. We won't code your project for you. At some point (which is now) you have to do some reading to make that little step to be able to code things on your own. Think what is needed to perform such an animation - for sure you need to hold the current state of the fill somewhere in the item and have a method to change that state to the next one. And then you need to periodically call that method that will change the state. If you don't know anything about programming then please post in the newbie section. If you do, then think how to solve your problem - you have already been given the tools you need.
    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.


  9. #9
    Join Date
    Jun 2007
    Posts
    23
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: Filling a QGraphicsItem

    Something like this:
    Qt Code:
    1. #ifndef FRACTIONPOLYGONITEM_H_
    2. #define FRACTIONPOLYGONITEM_H_
    3.  
    4. #include <QGraphicsPolygonItem>
    5.  
    6. class FractionPolygonItem: public QGraphicsPolygonItem {
    7. public:
    8. FractionPolygonItem(qreal fraction, const QPolygonF &polygon, QGraphicsItem *parent = 0);
    9.  
    10. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    11. void setFraction(qreal fraction);
    12.  
    13. private:
    14. qreal fract;
    15. };
    16.  
    17. #endif /* FRACTIONPOLYGONITEM_H_ */
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "FractionPolygonItem.h"
    2.  
    3. #include <QPainter>
    4.  
    5. FractionPolygonItem::FractionPolygonItem(qreal fraction,
    6. const QPolygonF &polygon,
    7. QGraphicsItem *parent) :
    8. QGraphicsPolygonItem(polygon, parent), fract(fraction) {
    9. }
    10.  
    11. void FractionPolygonItem::paint(QPainter *painter,
    12. const QStyleOptionGraphicsItem *option,
    13. QWidget *widget) {
    14. // fill part of polygon
    15. painter->setPen(Qt::NoPen);
    16. painter->setBrush(brush());
    17. QRectF rect = boundingRect();
    18. rect.setTop(rect.bottom() - fract * rect.height());
    19. painter->drawPolygon(polygon().intersected(rect), fillRule());
    20. // draw polygon outline
    21. painter->setPen(pen());
    22. painter->setBrush(Qt::NoBrush);
    23. painter->drawPolygon(polygon(), fillRule());
    24. }
    25.  
    26. void FractionPolygonItem::setFraction(qreal fraction) {
    27. fract = fraction;
    28. update();
    29. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef FILLITEM_H
    2. #define FILLITEM_H
    3.  
    4. #include <QWidget>
    5. #include <QList>
    6.  
    7. class FractionPolygonItem;
    8.  
    9. class FillItem: public QWidget {
    10. Q_OBJECT
    11.  
    12. public:
    13. FillItem(QWidget *parent = 0);
    14.  
    15. private slots:
    16. void setFraction(int fraction);
    17.  
    18. private:
    19. void createItems();
    20.  
    21. QList<FractionPolygonItem*> items;
    22. };
    23.  
    24. #endif // FILLITEM_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "FillItem.h"
    2.  
    3. #include <QVBoxLayout>
    4. #include <QGraphicsView>
    5. #include <QSlider>
    6.  
    7. #include "FractionPolygonItem.h"
    8.  
    9. FillItem::FillItem(QWidget *parent) :
    10. QWidget(parent) {
    11. createItems();
    12. // create scene and populate it with items
    13. QGraphicsScene *scene = new QGraphicsScene(this);
    14. foreach(FractionPolygonItem *item, items)
    15. scene->addItem(item);
    16. // create view and configure it
    17. QGraphicsView *view = new QGraphicsView(scene, this);
    18. view->setRenderHint(QPainter::Antialiasing, true);
    19. view->scale(20, 20);
    20. // create slider and configure it
    21. QSlider *slider = new QSlider(Qt::Horizontal, this);
    22. slider->setRange(0, 100);
    23. slider->setValue(60);
    24. connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setFraction(int)));
    25. setFraction(slider->value());
    26. // add view and slider
    27. QVBoxLayout *layout = new QVBoxLayout(this);
    28. layout->addWidget(view);
    29. layout->addWidget(slider);
    30. }
    31.  
    32. void FillItem::setFraction(int fraction) {
    33. foreach(FractionPolygonItem *item, items)
    34. item->setFraction(fraction / 100.0);
    35. }
    36.  
    37. void FillItem::createItems() {
    38. QPolygonF poly;
    39. FractionPolygonItem *item;
    40. // first item
    41. poly << QPointF(0, 0) << QPointF(1, 0) << QPointF(1.5, 1.0);
    42. poly << QPointF(0.5, 2.0) << QPointF(-0.5, 1.0);
    43. item = new FractionPolygonItem(0, poly);
    44. item->setBrush(QBrush(Qt::blue));
    45. items.append(item);
    46. // second item
    47. poly.clear();
    48. poly << QPointF(0, 0) << QPointF(1, 0) << QPointF(1.5, 2) << QPointF(-0.5, 2);
    49. item = new FractionPolygonItem(0, poly);
    50. item->setBrush(QBrush(Qt::red));
    51. item->translate(4, 0);
    52. items.append(item);
    53. }
    To copy to clipboard, switch view to plain text mode 

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


  10. The following user says thank you to kamre for this useful post:

    c_srikanth1984 (8th July 2009)

Similar Threads

  1. QGraphicsItem doesn't inherit QObject?
    By xyzt in forum Qt Programming
    Replies: 6
    Last Post: 26th September 2011, 14:59
  2. Replies: 2
    Last Post: 25th March 2011, 09:18
  3. destruction of QGraphicsItem
    By killkolor in forum Qt Programming
    Replies: 2
    Last Post: 5th December 2009, 10:31
  4. Replies: 11
    Last Post: 2nd July 2009, 00:41
  5. Replies: 1
    Last Post: 25th February 2009, 00:34

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.