Something like this:
#ifndef FRACTIONPOLYGONITEM_H_
#define FRACTIONPOLYGONITEM_H_
#include <QGraphicsPolygonItem>
public:
void setFraction(qreal fraction);
private:
qreal fract;
};
#endif /* FRACTIONPOLYGONITEM_H_ */
#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_ */
To copy to clipboard, switch view to plain text mode
#include "FractionPolygonItem.h"
#include <QPainter>
FractionPolygonItem::FractionPolygonItem(qreal fraction,
}
void FractionPolygonItem
::paint(QPainter *painter,
// 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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
#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
#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
To copy to clipboard, switch view to plain text mode
#include "FillItem.h"
#include <QVBoxLayout>
#include <QGraphicsView>
#include <QSlider>
#include "FractionPolygonItem.h"
FillItem
::FillItem(QWidget *parent
) : createItems();
// create scene and populate it with items
foreach(FractionPolygonItem *item, items)
scene->addItem(item);
// create view and configure it
view
->setRenderHint
(QPainter::Antialiasing,
true);
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() {
FractionPolygonItem *item;
// first item
item = new FractionPolygonItem(0, poly);
item
->setBrush
(QBrush(Qt
::blue));
items.append(item);
// second item
poly.clear();
item = new FractionPolygonItem(0, poly);
item
->setBrush
(QBrush(Qt
::red));
item->translate(4, 0);
items.append(item);
}
#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);
}
To copy to clipboard, switch view to plain text mode
#include "fillitem.h"
#include <QtGui>
#include <QApplication>
int main(int argc, char *argv[]) {
FillItem w;
w.show();
return a.exec();
}
#include "fillitem.h"
#include <QtGui>
#include <QApplication>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
FillItem w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks