Hier mal ein kleines Beispiel:
myitem.h
Code: Alles auswählen
#ifndef _MYITEM_
#define _MYITEM_
#include <QGraphicsItem>
class MyItem : public QGraphicsItem
{
public:
MyItem( QGraphicsItem *parent = 0 ) : QGraphicsItem(parent)
{ setFlags( QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable ); }
void paint( QPainter *, const QStyleOptionGraphicsItem *option, QWidget *widget=0 );
QRectF boundingRect() const;
private:
QPointF pointForAngle( double, double );
};
#endif // _MYITEM_
myitem.cpp
Code: Alles auswählen
#include "myitem.h"
#include <QPainter>
#include <QBrush>
#include <QRectF>
#include <QGraphicsScene>
#include <QRadialGradient>
#include <cmath>
void MyItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
Q_UNUSED(option);
Q_UNUSED(widget);
QRadialGradient gradient( QPointF(0,0), 100, QPointF(0,0) );
if( this == scene()->mouseGrabberItem() ) {
gradient.setColorAt( 0.0, Qt::red );
gradient.setColorAt( 1.0, Qt::yellow );
} else {
gradient.setColorAt( 1.0, Qt::red );
gradient.setColorAt( 0.0, Qt::yellow );
}
painter->setBrush( gradient );
QPainterPath path;
int _spikes = 7;
double angle1 = 360.0 / (double)_spikes;
double angle2 = angle1 / 2.0;
double angle;
double rad1 = 100.0;
double rad2 = 70.0;
QPointF c1, c2, end;
end = pointForAngle( -90, rad1 );
path.moveTo( end );
for( int i=0; i<_spikes; i++ ) {
angle = i * angle1 - 90;
end = pointForAngle( angle + angle2, rad2 );
c1 = pointForAngle( angle + angle2/4, rad2 + 5.0 );
c2 = pointForAngle( angle + angle2 - angle2/4, rad1 - 5.0 );
path.cubicTo( c1, c2, end );
end = pointForAngle( angle + angle1, rad1 );
c1 = pointForAngle( angle + angle2 + angle2/4, rad1 - 5.0 );
c2 = pointForAngle( angle + angle1 - angle2/4, rad2 + 5.0 );
path.cubicTo( c1, c2, end );
}
painter->drawPath( path );
}
QRectF MyItem::boundingRect() const
{
return QRectF( -100, -100, 200, 200 );
}
QPointF MyItem::pointForAngle( double angle, double radius )
{
double radian = angle * 2 * M_PI / 360;
double x = cos( radian ) * radius;
double y = sin( radian ) * radius;
return QPointF( x, y );
}
main.cpp
Code: Alles auswählen
#include "myitem.h"
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QApplication>
int main( int argc, char **argv )
{
QApplication app( argc, argv );
MyItem *item1 = new MyItem;
MyItem *item2 = new MyItem;
QGraphicsScene *scene = new QGraphicsScene;
scene->addItem( item1 );
scene->addItem( item2 );
QGraphicsView view;
view.setScene( scene );
view.setMinimumWidth( 500 );
view.setMinimumHeight( 500 );
view.show();
return app.exec();
}
Grüße
Franz
// edit:
Wie du siehst hab ich keinen QBrush & Co gespeichert, da ich aus deinem kleinen Code-Beispiel entnommen hab, dass du auf Maus-Klicks reagieren willst. Genau das hab ich jetzt gemacht. In anderen Fällen (ohne Maus-Klick, sondern von außen setzen) musst du dir aber schon die Farbe merken, da du nur in der paint()-Methode Zugriff auf den QPainter hast, mit dem gezeichnet wird, und du ja diesem deinen Brush zuweisen musst.