Всем привет. Есть такой код:
class Widget : public QWidget
{
Q_OBJECT
QPoint mouseOldPos;
QPoint mousePos;
QImage *image;
public:
Widget( QWidget *parent = 0 );
~Widget();
protected:
void paintEvent( QPaintEvent *e );
void draw( QPainter *painter );
void mousePressEvent( QMouseEvent *e );
void mouseReleaseEvent( QMouseEvent *e );
void mouseMoveEvent( QMouseEvent *e );
protected slots:
void createImage();
};
Widget::Widget( QWidget *parent ):
QWidget( parent ),
image(0)
{
setAttribute( Qt::WA_StaticContents );
setCursor( Qt::CrossCursor );
setMouseTracking( true );
setFocusPolicy( Qt::WheelFocus );
setFocus();
setFixedSize( 800, 600 );
QTimer::singleShot( 500, this, SLOT(createImage()) );
}
//================================================================================
=================
Widget::~Widget()
{
if (image)
{
delete image;
image = 0;
}
}
//================================================================================
=================
void Widget::paintEvent( QPaintEvent *e )
{
if (image)
{
QPainter imagePainter( image );
imagePainter.setRenderHint( QPainter::Antialiasing, true );
imagePainter.eraseRect( rect() );
draw( &imagePainter );
imagePainter.end();
QPainter painter( this );
QRect dirtyRect = e->rect();
painter.drawImage( dirtyRect, (*image), dirtyRect );
}
}
//================================================================================
=================
void Widget::draw( QPainter *painter )
{
painter->save();
painter->translate( width() / 2, height() / 2 );
painter->setPen( QColor( 0, 0, 0, 255 ) );
painter->drawEllipse( QPoint( 0, 90 ), 90, 90 );
painter->restore();
painter->save();
painter->setPen( QPen( Qt::green, 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin ) );
painter->drawLine( QPoint( width() / 2, height() / 2 + 90 ), mousePos );
painter->restore();
}
//================================================================================
=================
void Widget::mousePressEvent( QMouseEvent *e )
{
if (e->buttons() & Qt::LeftButton)
{
mouseOldPos = e->pos();
update();
}
}
//================================================================================
=================
void Widget::mouseReleaseEvent( QMouseEvent *e )
{
update();
e->accept();
}
//================================================================================
=================
void Widget::mouseMoveEvent( QMouseEvent *e )
{
mousePos = e->pos();
update();
}
//================================================================================
=================
void Widget::createImage()
{
if (!image)
{
image = new QImage( size(), QImage::Format_ARGB32_Premultiplied );
image->fill( Qt::white );
update();
}
}
Он рисует окружность и зелёную линию от центра до указателя мыши.
Нужно чтобы рисовалась часть линии, выделенная красным цветом.
В какую сторону копать?
Image