Jonny174
15th March 2012, 03:05
Hi All!
I have a class:
widget.h
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.cpp
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();
}
}
How can I draw part of line, selected in red?
7504
http://saveimg.ru/show-image.php?id=8c69bcea7bba744e7b2f4d7b2c54d30a
I have a class:
widget.h
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.cpp
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();
}
}
How can I draw part of line, selected in red?
7504
http://saveimg.ru/show-image.php?id=8c69bcea7bba744e7b2f4d7b2c54d30a