Results 1 to 4 of 4

Thread: draw point with mouseevent

  1. #1

    Default 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:

    Qt Code:
    1. class StarItem : public QGraphicsItem
    2. {
    3. public:
    4. StarItem( QGraphicsItem *parent = 0 ) : QGraphicsItem(parent) {}
    5. void paint( QPainter *, const QStyleOptionGraphicsItem *, QWidget *widget=0 );
    6. QRectF boundingRect() const;
    7. protected:
    8. void mousePressEvent(QGraphicsSceneMouseEvent *e);
    9. void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
    10. private:
    11. QPoint position;
    12. int posx;
    13. int posy;
    14. bool shower;
    15. };
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. #include "staritem.h"
    2.  
    3. void StarItem::mousePressEvent(QGraphicsSceneMouseEvent *e)
    4. {
    5. if (e->button() == Qt::LeftButton)
    6. {
    7. shower=true;
    8. QPoint position = e->screenPos();
    9.  
    10. posx = position.x();
    11. posy = position.y();
    12. update();
    13. }
    14. return QGraphicsItem::mousePressEvent(e);
    15. }
    16.  
    17. void StarItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
    18. {
    19. position = e->screenPos();
    20. return QGraphicsItem::mouseMoveEvent(e);
    21. }
    22.  
    23.  
    24. void StarItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget )
    25. {
    26. Q_UNUSED( option );
    27. Q_UNUSED( widget );
    28.  
    29. QPen pen(Qt::black, 6, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
    30. painter->setPen(pen);
    31.  
    32. if(shower=true)
    33. {
    34. painter->drawPoint(posx,posy);
    35. }
    36.  
    37. }
    38.  
    39. QRectF StarItem::boundingRect() const
    40. {
    41. return QRectF( 0, 0, 100, 100 );
    42. }
    To copy to clipboard, switch view to plain text mode 

    I have no idea what's wrong...

  2. #2
    Join Date
    Aug 2008
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: draw point with mouseevent

    Hi,

    I just saw the problem:

    Qt Code:
    1. if(shower=true)
    2. {
    3. painter->drawPoint(posx,posy);
    4. }
    To copy to clipboard, switch view to plain text mode 

    I think you want:

    Qt Code:
    1. shower == true
    To copy to clipboard, switch view to plain text mode 

    Have fun!

    -draftpunk

  3. #3

    Default 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
    Qt Code:
    1. int main(int argc,char **argv)
    2. {
    3. QApplication app(argc,argv);
    4.  
    5. StarItem item(100,100,10,10);
    6. scene.addItem(&item);
    7.  
    8. view.setGeometry(100, 100, 500, 500);
    9. view.setScene(&scene);
    10. view.show();
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 
    staritem.h
    Qt Code:
    1. class StarItem : public QGraphicsEllipseItem
    2. {
    3. public:
    4. StarItem (int x,int y,int w,int h) : QGraphicsEllipseItem (x,y,w,h) {}
    5. protected:
    6. void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
    7. };
    To copy to clipboard, switch view to plain text mode 
    staritem.cpp
    Qt Code:
    1. void StarItem::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
    2. {
    3. setRect((e->scenePos().x()),(e->scenePos().y()),10,10);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for every answer.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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

Similar Threads

  1. Check a point inside or outside a road?
    By kstking in forum Qt Programming
    Replies: 1
    Last Post: 15th October 2007, 18:48
  2. Moving the (0.0) point on the scene
    By maverick_pol in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2007, 15:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.