PDA

View Full Version : Filling a QGraphicsItem



c_srikanth1984
2nd July 2009, 10:07
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.

wysota
2nd July 2009, 10:16
Reimplement the paint routine for the item and paint it properly. What did you try already?

c_srikanth1984
2nd July 2009, 10:43
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.

wysota
2nd July 2009, 11:01
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().

c_srikanth1984
2nd July 2009, 11:14
But I want to retain its original shape.

wysota
2nd July 2009, 11:16
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.

c_srikanth1984
2nd July 2009, 11:21
if I impliment in this loop my Item should be filled in 5 loops as in attached image

c_srikanth1984
3rd July 2009, 05:12
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.

aamer4yu
3rd July 2009, 08:09
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.

c_srikanth1984
3rd July 2009, 09:36
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.

wysota
3rd July 2009, 11:23
QGraphicsItem::update()

c_srikanth1984
4th July 2009, 07:10
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.

wysota
4th July 2009, 07:54
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.

c_srikanth1984
4th July 2009, 08:32
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.

wysota
4th July 2009, 09:05
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.

kamre
6th July 2009, 14:34
Something like this:


#ifndef FRACTIONPOLYGONITEM_H_
#define FRACTIONPOLYGONITEM_H_

#include <QGraphicsPolygonItem>

class FractionPolygonItem: public QGraphicsPolygonItem {
public:
FractionPolygonItem(qreal fraction, const QPolygonF &polygon, QGraphicsItem *parent = 0);

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
void setFraction(qreal fraction);

private:
qreal fract;
};

#endif /* FRACTIONPOLYGONITEM_H_ */




#include "FractionPolygonItem.h"

#include <QPainter>

FractionPolygonItem::FractionPolygonItem(qreal fraction,
const QPolygonF &polygon,
QGraphicsItem *parent) :
QGraphicsPolygonItem(polygon, parent), fract(fraction) {
}

void FractionPolygonItem::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option,
QWidget *widget) {
// fill part of polygon
painter->setPen(Qt::NoPen);
painter->setBrush(brush());
QRectF rect = boundingRect();
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();
}




#ifndef FILLITEM_H
#define FILLITEM_H

#include <QWidget>
#include <QList>

class FractionPolygonItem;

class FillItem: public QWidget {
Q_OBJECT

public:
FillItem(QWidget *parent = 0);

private slots:
void setFraction(int fraction);

private:
void createItems();

QList<FractionPolygonItem*> items;
};

#endif // FILLITEM_H




#include "FillItem.h"

#include <QVBoxLayout>
#include <QGraphicsView>
#include <QSlider>

#include "FractionPolygonItem.h"

FillItem::FillItem(QWidget *parent) :
QWidget(parent) {
createItems();
// create scene and populate it with items
QGraphicsScene *scene = new QGraphicsScene(this);
foreach(FractionPolygonItem *item, items)
scene->addItem(item);
// create view and configure it
QGraphicsView *view = new QGraphicsView(scene, this);
view->setRenderHint(QPainter::Antialiasing, true);
view->scale(20, 20);
// create slider and configure it
QSlider *slider = new QSlider(Qt::Horizontal, this);
slider->setRange(0, 100);
slider->setValue(60);
connect(slider, SIGNAL(valueChanged(int)), this, SLOT(setFraction(int)));
setFraction(slider->value());
// add view and slider
QVBoxLayout *layout = new QVBoxLayout(this);
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
poly << QPointF(0, 0) << QPointF(1, 0) << QPointF(1.5, 1.0);
poly << QPointF(0.5, 2.0) << QPointF(-0.5, 1.0);
item = new FractionPolygonItem(0, poly);
item->setBrush(QBrush(Qt::blue));
items.append(item);
// second item
poly.clear();
poly << QPointF(0, 0) << QPointF(1, 0) << QPointF(1.5, 2) << QPointF(-0.5, 2);
item = new FractionPolygonItem(0, poly);
item->setBrush(QBrush(Qt::red));
item->translate(4, 0);
items.append(item);
}




#include "fillitem.h"

#include <QtGui>
#include <QApplication>

int main(int argc, char *argv[]) {
QApplication a(argc, argv);
FillItem w;
w.show();
return a.exec();
}


http://pic.ipicture.ru/uploads/090706/1vEwOmQ0Uo.png