I am trying to create a simple painting application - it is just supposed to draw when you click and drag a cursor (just like Paint). I know I have to use
It's a pretty broad question, but here are the basics:
QWidgetclass, so that you can override some of its virtual methods later on.
show()on it (just before calling
QApplication::exec()). This object will appear on screen as a very simple window, and it will serve as your user's painting-surface.
QPixmapobject that you will use to store the bitmap that the user will draw. Make sure to size the
QPixmapto be at least as big as the maximum size of the window that you want to support. Call
QPixmapto fill it with your favorite background color.
mousePressEvent(QMouseEvent *)method of your object to set a boolean is_mouse_down flag, and also to record the current position of the mouse pointer within the window (by calling
QMouseEventobject that gets passed in to the
mousePressEvent()call and storing that into a member variable of your object).
mouseMoveEvent(QMouseEvent *)method so that if
true, it creates a
QPainterobject on the stack -- pass a pointer to the
QPainterobject's constructor so that the
QPainterwill draw into your
QPixmapobject. Then call
QPainterobject to draw a line from the previous mouse position to your current one. Finally, call
update()to tell Qt to call
paintEvent()for you ASAP.
mouseReleaseEvent(QMouseEvent *)method to set
is_mouse_downto false again
paintEvent(QPaintEvent *)method to create a
QPainterobject on the stack -- pass a pointer to (
this) to the
QPainterobject's constructor, so that it will paint onto the
QWidgetdirectly. Then call drawPixmap() on the
QPainterobject so that it will draw your
QPixmapobject onto the widget's visible surface.
If you'd like to see a pre-written example, check out the Scribble application included with Qt, in