draw point with mouseevent
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:
Code:
{
public:
protected:
private:
int posx;
int posy;
bool shower;
};
Code:
#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 );
}
I have no idea what's wrong...
Re: draw point with mouseevent
Hi,
I just saw the problem:
Code:
if(shower=true)
{
painter->drawPoint(posx,posy);
}
I think you want:
Have fun!
-draftpunk
Re: draw point with mouseevent
Thanks for the answer.
but I now the problem, I have to draw in the graphicscene and not in der item...
But I tried another way without solution: Now I try to move a ellipse that is in the scene, but in my program there's a small mistake(I think), what I don't see...
I think it's a mistake in the main function?!
Please help...
main.cpp
Code:
int main(int argc,char **argv)
{
StarItem item(100,100,10,10);
scene.addItem(&item);
view.setGeometry(100, 100, 500, 500);
view.setScene(&scene);
view.show();
return app.exec();
}
staritem.h
Code:
{
public:
protected:
};
staritem.cpp
Code:
{
setRect((e->scenePos().x()),(e->scenePos().y()),10,10);
}
Thanks for every answer.
Re: draw point with mouseevent
what are you trying to achieve ? If you want to move the item, you dont need to code !!
Qt does it for u.. just have a look at QGraphicsItem::setFlag with value QGraphicsItem::ItemIsMovable :)