Qt-旋转图案


taowidget.h

#ifndef _TAOWIDGET_H_
#define _TAOWIDGET_H_

#include <QWidget>

class QTimer;

class TaoWidget : public QWidget
{
public:
    TaoWidget( QWidget * = 0 );
    
protected:
    void paintEvent( QPaintEvent * );

private:
    QTimer *timer;
    int degree;
};

#endif 

taowidget.cpp

#include "taowidget.h"
#include <QtGui>

TaoWidget::TaoWidget( QWidget *parent )
    :QWidget( parent ), degree( 0 )
{
    timer = new QTimer( this );
    connect( timer, SIGNAL( timeout() ), this, SLOT( update() ) );
    timer->start( 10 );
}

void TaoWidget::paintEvent( QPaintEvent * )
{
    QPainter painter( this );
    QPen blackPen( Qt::black, 2 );
    painter.setPen( blackPen );
    painter.setRenderHint( QPainter::Antialiasing, true );

    painter.translate( width() / 2.0, height() / 2.0 );
    painter.rotate( degree );

    degree  = ( degree + 1 ) % 360;
    
    int side = qMin( width(), height() ) - 4;
    QRectF rect( -side / 2.0, - side / 2.0, side, side );
    painter.drawEllipse( rect );

    painter.setBrush( Qt::black );
    painter.setPen( Qt::NoPen );
    painter.drawPie( rect, -90 * 16, 180 * 16 );

    QPointF center = rect.center();
    QRectF bottomRect( center.x() - side / 4.0,
		       center.y() + 1.0, side / 2.0, side / 2.0 );
    painter.drawEllipse( bottomRect );

    painter.setBrush( Qt::white );
    QRectF topRect( center.x() - side / 4.0,
		    rect.top() + 1.0, side / 2.0, side / 2.0 );
    painter.drawEllipse( topRect );

    QPointF bottomCenter = bottomRect.center();
    QRectF whiteRect( bottomCenter.x() - side / 20.0, bottomCenter.y() - side/20.0,
		               side / 10.0, side / 10.0 );
    painter.drawEllipse( whiteRect );

    QPointF topCenter = topRect.center();
    QRectF blackRect( topCenter.x() - side / 20.0, topCenter.y() - side / 20.0,
		               side / 10.0, side / 10.0 );
    painter.setBrush( Qt::black );
    painter.drawEllipse( blackRect );
}
main.cpp

#include "taowidget.h"
#include <QApplication>

int main(int argc, char **argv )
{
    QApplication app( argc, argv );

    TaoWidget widget;
    widget.show();
    
    return app.exec();
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值