PDA

View Full Version : QPainter: line drawing along a circle



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

ChrisW67
15th March 2012, 03:41
Try again. The attachment is broken.

Oh, and have a look at QPainter::drawArc() just in case that's what you were looking for.


Edit: Now you have posted a picture I have no idea what you want. What does "along a circle" have to do with the straight line segment in red? Seems like a simple case of calculate the end points, set a pen, draw a line.

Jonny174
15th March 2012, 05:54
SOLVE:


float mouse_radius = getDistanceBetweenPoints( width() / 2, height() / 2 + 90, mousePos.x(), mousePos.y() );

float pX = width() / 2 + 90 * (mousePos.x() - width() / 2) / mouse_radius;
float pY = (height() / 2 + 90) + 90 * (mousePos.y() - (height() / 2 + 90)) / mouse_radius;

float pX2 = width() / 2 + 80 * (mousePos.x() - width() / 2) / mouse_radius;
float pY2 = (height() / 2 + 90) + 80 * (mousePos.y() - (height() / 2 + 90)) / mouse_radius;

painter->drawLine( int( pX2 ), int( pY2 ), int( pX ), int( pY ) );



float getVectorLength( float dx, float dy )
{
return qSqrt( dx * dx + dy * dy );
}

float getDistanceBetweenPoints( float x1, float y1, float x2, float y2 )
{
return getVectorLength( x2 - x1, y2 - y1 );
}