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.
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.
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.
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:
{ if (m_active) painter.drawPath(somePath); } { m_active = !m_active; update(); }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.
Niamita (7th June 2011)
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?
You can add a timer and clear it when not required
Qt Code:
{ m_active = true; update(); startTimer(2000); //2 seconds } { m_active = false; update(); killTimer(event->timerId()); }To copy to clipboard, switch view to plain text mode
Bookmarks