Hallo, I'm a beginner and use Qt 4.4.3.
I would like to draw a point in a scene when i click in the scene. but my program draws a point without clicking, but the point should appear if i click.
I hope somebody can help me.
Here is my program:
{
public:
protected:
private:
int posx;
int posy;
bool shower;
};
class StarItem : public QGraphicsItem
{
public:
StarItem( QGraphicsItem *parent = 0 ) : QGraphicsItem(parent) {}
void paint( QPainter *, const QStyleOptionGraphicsItem *, QWidget *widget=0 );
QRectF boundingRect() const;
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *e);
void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
private:
QPoint position;
int posx;
int posy;
bool shower;
};
To copy to clipboard, switch view to plain text mode
#include "staritem.h"
{
if (e->button() == Qt::LeftButton)
{
shower=true;
QPoint position
= e
->screenPos
();
posx = position.x();
posy = position.y();
update();
}
}
{
position = e->screenPos();
}
{
Q_UNUSED( option );
Q_UNUSED( widget );
QPen pen
(Qt
::black,
6, Qt
::SolidLine, Qt
::RoundCap, Qt
::RoundJoin);
painter->setPen(pen);
if(shower=true)
{
painter->drawPoint(posx,posy);
}
}
QRectF StarItem
::boundingRect() const {
return QRectF( 0,
0,
100,
100 );
}
#include "staritem.h"
void StarItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
{
if (e->button() == Qt::LeftButton)
{
shower=true;
QPoint position = e->screenPos();
posx = position.x();
posy = position.y();
update();
}
return QGraphicsItem::mousePressEvent(e);
}
void StarItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
{
position = e->screenPos();
return QGraphicsItem::mouseMoveEvent(e);
}
void StarItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
{
Q_UNUSED( option );
Q_UNUSED( widget );
QPen pen(Qt::black, 6, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
painter->setPen(pen);
if(shower=true)
{
painter->drawPoint(posx,posy);
}
}
QRectF StarItem::boundingRect() const
{
return QRectF( 0, 0, 100, 100 );
}
To copy to clipboard, switch view to plain text mode
I have no idea what's wrong...
Bookmarks