#include <QApplication>#include <QPainter>#include <QImage>#include <QMouseEvent>#include <QPaintEvent>#include <QWidget>class Pa : public QWidget{public: Pa() { pixmap_ = QPixmap( 4096, 4096 ); pixmap_.fill( 0xffffffff ); pen_ = QPen( QBrush( Qt::black ), 5.0f ); }protected: void mousePressEvent( QMouseEvent * e ) { if ( e->button() == Qt::LeftButton ) { QPainter p( &pixmap_ ); p.setPen( pen_ ); p.drawPoint( e->pos() ); p.end(); lastPoint_ = e->pos(); update(); } } void mouseMoveEvent( QMouseEvent * e ) { if ( e->buttons() & Qt::LeftButton ) { QPainter p( &pixmap_ ); p.setPen( pen_ ); p.drawLine( lastPoint_, e->pos() ); p.end(); lastPoint_ = e->pos(); update(); } } void paintEvent( QPaintEvent * ) { QPainter p( this ); p.setCompositionMode( QPainter::CompositionMode_Source ); p.drawPixmap( QPoint(), pixmap_ ); }private: QPen pen_; QPixmap pixmap_; QPoint lastPoint_;};int main( int argc, char ** argv ){ QApplication app( argc, argv ); Pa pa; pa.show(); return app.exec();}