Results 1 to 5 of 5

Thread: Draw shape on mousepress?

  1. #1
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Draw shape on mousepress?

    Hi
    i want to draw a shape when the particular area is pressed.
    I want to ask that how to combine paintEvent() and mousePressEvent() method to implement above.

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Draw shape on mousepress?

    In your mousePressEvent() get the coordinates of the event and store them in some variable. Then call this->update(), which will launch your paintEvent() where you can do the drawing or call your draqing method.

  3. #3
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: Draw shape on mousepress?

    There are numerous ways to achieve this and you'll need to provide a bit more information to get some useful help. For example, what is a "particular area" in this case?

    Let's say you are creating a drawing program, adding shapes every time the user clicks a specific location; then you could use a QGraphicsScene and simply add items to that scene inside the mousePressEvent.

    Or, if you have a layout with several widgets and simply want to toggle the visible state of each widget when it is clicked, then it's probably better to reimplement paintEvent and mousePressEvent in that specific widget and have a member variable that keeps track of whether or not the widget is "active".

    Qt Code:
    1. void paintEvent(QPaintEvent *)
    2. {
    3. QPainter painter(this);
    4. if (m_active)
    5. painter.drawPath(somePath);
    6. }
    7.  
    8. void mousePressEvent(QMouseEvent *)
    9. {
    10. m_active = !m_active;
    11. update();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Or, (most likely) if you have a single widget and want to draw a shape where the mouse press event occurs; like mvuori said in previous post.

    Again, it all depends on what you mean by "particular area" and what the rest of you application looks like.

  4. The following user says thank you to helloworld for this useful post:

    Niamita (7th June 2011)

  5. #4
    Join Date
    May 2011
    Posts
    120
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Draw shape on mousepress?

    hi helloworld
    i do the same as u say and i am getting which i wanted to do , after pressed the painted shape is permanent on the area while i want that after pressing the area the painted shape visible only for few seconds . How i can do this.
    Does anybody can tell me how to do this?

  6. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Draw shape on mousepress?

    You can add a timer and clear it when not required

    Qt Code:
    1. void mousePressEvent(QMouseEvent *)
    2. {
    3. m_active = true;
    4. update();
    5. startTimer(2000); //2 seconds
    6. }
    7.  
    8. void timerEvent(QTimerEvent* event)
    9. {
    10. m_active = false;
    11. update();
    12. killTimer(event->timerId());
    13. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. get mousepress when widget is disabled (QLineEdit)
    By mortoray in forum Qt Programming
    Replies: 3
    Last Post: 10th November 2010, 05:44
  2. mousepress event
    By tpthyd in forum Qt Programming
    Replies: 3
    Last Post: 15th May 2009, 22:16
  3. mousepress event
    By tpthyd in forum Qt Programming
    Replies: 1
    Last Post: 25th February 2009, 16:02
  4. Can Qt4 capture mousePress events on the desktop?
    By simula in forum Qt Programming
    Replies: 5
    Last Post: 6th January 2009, 20:10
  5. MousePress Events on ContextMenu items
    By Naveen in forum Qt Programming
    Replies: 1
    Last Post: 22nd February 2006, 08:26

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.