Hi,
I want to fill a GraphicsItem with a color but not completly i want to fill it based on user choice i.e if user want to fill only 50% of a Item the Item should get filled only partially.
How is this possible
Check the attached image.
Printable View
Hi,
I want to fill a GraphicsItem with a color but not completly i want to fill it based on user choice i.e if user want to fill only 50% of a Item the Item should get filled only partially.
How is this possible
Check the attached image.
Reimplement the paint routine for the item and paint it properly. What did you try already?
Thanks for reply.
Actually I am reimplementing the Diagram Scene Example. I want to fill only some region of graphics item. I have not found any methods to that, except setBrush which fill complete item.
My aim is to fill continuously from 0 to 100 % of an Item.
How can I do it.
I already told you how. Reimplement the paint() rountine, calculate the shape that is to be filled and use QPainter API to do the drawing, probably using QPainter::drawPolygon().
But I want to retain its original shape.
My answer still stands. And if you want to do it another way then apply clipping to the painter according to the part you want to fill and fill the whole item.
if I impliment in this loop my Item should be filled in 5 loops as in attached image
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.
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.
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.
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.
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.
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.
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.
Something like this:
Code:
#ifndef FRACTIONPOLYGONITEM_H_ #define FRACTIONPOLYGONITEM_H_ #include <QGraphicsPolygonItem> public: void setFraction(qreal fraction); private: qreal fract; }; #endif /* FRACTIONPOLYGONITEM_H_ */
Code:
#include "FractionPolygonItem.h" #include <QPainter> FractionPolygonItem::FractionPolygonItem(qreal fraction, } // fill part of polygon painter->setPen(Qt::NoPen); painter->setBrush(brush()); rect.setTop(rect.bottom() - fract * rect.height()); painter->drawPolygon(polygon().intersected(rect), fillRule()); // draw polygon outline painter->setPen(pen()); painter->setBrush(Qt::NoBrush); painter->drawPolygon(polygon(), fillRule()); } void FractionPolygonItem::setFraction(qreal fraction) { fract = fraction; update(); }
Code:
#ifndef FILLITEM_H #define FILLITEM_H #include <QWidget> #include <QList> class FractionPolygonItem; Q_OBJECT public: private slots: void setFraction(int fraction); private: void createItems(); QList<FractionPolygonItem*> items; }; #endif // FILLITEM_H
Code:
#include "FillItem.h" #include <QVBoxLayout> #include <QGraphicsView> #include <QSlider> #include "FractionPolygonItem.h" createItems(); // create scene and populate it with items foreach(FractionPolygonItem *item, items) scene->addItem(item); // create view and configure it view->scale(20, 20); // create slider and configure it slider->setRange(0, 100); slider->setValue(60); connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setFraction(int))); setFraction(slider->value()); // add view and slider layout->addWidget(view); layout->addWidget(slider); } void FillItem::setFraction(int fraction) { foreach(FractionPolygonItem *item, items) item->setFraction(fraction / 100.0); } void FillItem::createItems() { QPolygonF poly; FractionPolygonItem *item; // first item item = new FractionPolygonItem(0, poly); items.append(item); // second item poly.clear(); item = new FractionPolygonItem(0, poly); item->translate(4, 0); items.append(item); }
Code:
#include "fillitem.h" #include <QtGui> #include <QApplication> int main(int argc, char *argv[]) { FillItem w; w.show(); return a.exec(); }
http://pic.ipicture.ru/uploads/090706/1vEwOmQ0Uo.png