PDA

View Full Version : Graphics Item



Maluko_Da_Tola
20th July 2010, 12:52
hello, i am trying to create a graphics item. I created a class called wierdo using the following code:

#ifndef WIERDO_H
#define WIERDO_H

#include <QGraphicsItem>

class Wierdo : public QGraphicsItem
{

public:
QRectF boundingRect() const { return QRectF(0, 0,100, 100);};

QPainterPath shape() const{
QPainterPath path;
path.addRect(-10, -20, 20, 40);
return path;
};

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
painter->drawEllipse(-10, -20, 20, 40);
painter->drawEllipse(-10, -20, 20, 40);

};

#endif // WIERDO_H

The compiler does not accept the method painter->drawEllipse(-10, -20, 20, 40); saying that there is an "unexpected `(' token". Any suggestions on why this is happening?

Thank you

Lykurg
20th July 2010, 13:34
The compiler does not accept the method painter->drawEllipse(-10, -20, 20, 40); saying that there is an "unexpected `(' token". Any suggestions on why this is happening? It's like the compiler says to you: Your syntax is wrong! Basic C++...
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
painter->drawEllipse(-10, -20, 20, 40);
painter->drawEllipse(-10, -20, 20, 40);
}

..And you probably need to include the header for QPainter.

Maluko_Da_Tola
20th July 2010, 14:07
the only syntax error i can find is the missing parenthesis that enclose the method 'paint'. However, the compiler still display the same error (i.e. that the '' painter->drawEllipse(-10, -20, 20, 40);'' has an unexpected `(' token)!
I have now the following code:

#ifndef WIERDO_H
#define WIERDO_H

#include <QGraphicsItem>

class Wierdo : public QGraphicsItem
{

public:
QRectF boundingRect() const { return QRectF(0, 0,100, 100);};

QPainterPath shape() const{
QPainterPath path;
path.addRect(-10, -20, 20, 40);
return path;
};

void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
{
painter->drawEllipse(-10, -20, 20, 40);
painter->drawEllipse(-10, -20, 20, 40);
};

};

#endif // WIERDO_H


The syntax I used for ''painter->drawEllipse(-10, -20, 20, 40);'' was taken from the colliding mice example. Any suggestions on why this is happening?

Thank you

Lykurg
20th July 2010, 14:34
there is no problem with the drawEllipse function call. The problem is your semicolons! remove them. (only after } of course) Only the class ending } has a semicolon!

Lykurg
20th July 2010, 14:37
and compare your code
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget);
{
painter->drawEllipse(-10, -20, 20, 40);
painter->drawEllipse(-10, -20, 20, 40);
}; with mine:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
painter->drawEllipse(-10, -20, 20, 40);
painter->drawEllipse(-10, -20, 20, 40);
}.

Also note I am using the [CODE] tags which formats the source code in a nicer way...