C++ (Qt)#include <QPaintEvent> #include <QPainter>#include <QImage>#include <QTimer> #include <QtGlobal> const int SZ = 1000; MyScene::MyScene( QWidget * parent ): QWidget( parent ){ _pen.setWidth( 1 ); _pen.setColor( Qt::red ); _leftPressed = false; _image = new QImage( SZ, SZ, QImage::Format_ARGB32_Premultiplied ); _image->fill( Qt::white ); resize( SZ, SZ );} void MyScene::paintEvent( QPaintEvent * event ){ QPainter painter( this ); QRect rect( event->rect() ); painter.drawImage( rect, *_image, rect );} void MyScene::mousePressEvent( QMouseEvent * event ){ if ( event->button() == Qt::LeftButton ) { _leftPressed = true; _startPoint = event->pos(); }} void MyScene::mouseMoveEvent( QMouseEvent * event ){ if ( _leftPressed ) { _endPoint = event->pos(); QPainter painter( _image ); painter.drawLine( _startPoint, _endPoint ); QRect rect( qMin( _startPoint.x(), _endPoint.x() ), qMin( _startPoint.y(), _endPoint.y() ), qAbs( _startPoint.x() - _endPoint.x() ) + 1, qAbs( _startPoint.y() - _endPoint.y() ) + 1 ); update( rect ); _startPoint = _endPoint; }} void MyScene::mouseReleaseEvent( QMouseEvent *){ _leftPressed = false;}