⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 butterfly.cpp

📁 Linux窗口程序设计__Qt4精彩实例分析上面的源代码第二部分.以循序渐进的方式进行源代码,包括Qt4下面的图形与图画对话框QMainWindow的源代码.
💻 CPP
字号:
#include "butterfly.h"
#include <QtGui>

#include <math.h>

static const double PI = 3.14159265358979323846264338327950288419717;

Butterfly::Butterfly()
{ 
    pix_up.load(":/images/butterfly1.png");
    pix_down.load(":/images/butterfly2.png");
    up = true;
    startTimer(100);
    
}

QRectF
Butterfly::boundingRect() const
{
    qreal adjust = 2;
    return QRectF(-pix_up.width()/2-adjust,-pix_up.height()/2-adjust,
    			pix_up.width()+adjust*2,pix_up.height()+2*adjust);
}

void
Butterfly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(up)
    {
    	painter->drawPixmap(boundingRect().topLeft(),pix_up);
    	up = !up;
    }
    else
    {
    	painter->drawPixmap(boundingRect().topLeft(),pix_down);
    	up = !up;
    }
    	
}

void
Butterfly::timerEvent(QTimerEvent *)
{
    // edge controll
    qreal edgex = scene()->sceneRect().right()+boundingRect().width()/2;
    qreal edgetop = scene()->sceneRect().top()+boundingRect().height()/2;
    qreal edgebottom = scene()->sceneRect().bottom()+boundingRect().height()/2;
    
    if (pos().x() >= edgex)
    	setPos(scene()->sceneRect().left(),pos().y());
    if (pos().y() <= edgetop)
        setPos(pos().x(),scene()->sceneRect().bottom());
    if (pos().y() >= edgebottom)
        setPos(pos().x(),scene()->sceneRect().top());
    
    angle += (qrand()%10)/20.0;
    qreal dx = fabs(sin(angle*PI)*10.0);    
    qreal dy = (qrand()%20)-10.0; 
    
    setPos(mapToParent(dx,dy));

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -