找回密码
 立即注册
收起左侧

请教大家QGraphicsItem 类的paint函数

2
回复
7657
查看
[复制链接]
累计签到:10 天
连续签到:1 天
来源: 2016-4-2 20:36:10 显示全部楼层 |阅读模式
5Qter豆
  1. #ifndef BUTTERFLY_H
  2. #define BUTTERFLY_H

  3. #include <QObject>
  4. #include <QGraphicsItem>
  5. #include <QPainter>
  6. #include <QGraphicsScene>
  7. #include <QGraphicsView>

  8. class Butterfly : public QObject,public QGraphicsItem
  9. {
  10.     Q_OBJECT
  11. public:
  12.     explicit Butterfly(QObject *parent = 0);
  13.     void timerEvent(QTimerEvent *);
  14.     QRectF boundingRect() const;  
  15.    
  16. signals:
  17.    
  18. public slots:
  19. protected:
  20.     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

  21. private:
  22.     bool up;
  23.     QPixmap pix_up;            //鐢ㄤ簬琛ㄧず涓ゅ箙铦磋澏鐨勫浘鐗
  24.     QPixmap pix_down;

  25.     qreal angle;
  26. };

  27. #endif // BUTTERFLY_H

复制代码
  1. #include "butterfly.h"
  2. #include <math.h>

  3. const static double PI=3.1416;

  4. Butterfly::Butterfly(QObject *parent) :
  5.     QObject(parent)
  6. {
  7.     up = true;
  8.     pix_up.load("D://CH7/CH701/Butterfly/up.jpg");
  9.     pix_down.load("D://CH7/CH701/Butterfly/down.jpg");

  10.     startTimer(100);
  11. }

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

  17. void Butterfly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  18. {
  19.     if(up)
  20.     {
  21.         painter->drawPixmap(boundingRect().topLeft(),pix_up);
  22.         up=!up;
  23.     }
  24.     else
  25.     {
  26.         painter->drawPixmap(boundingRect().topLeft(),pix_down);
  27.         up=!up;
  28.     }
  29. }

  30. void Butterfly::timerEvent(QTimerEvent *)
  31. {   
  32.     qreal edgex=scene()->sceneRect().right()+boundingRect().width()/2;
  33.     qreal edgetop=scene()->sceneRect().top()+boundingRect().height()/2;
  34.     qreal edgebottom=scene()->sceneRect().bottom()+boundingRect(). height()/2;

  35.     if(pos().x()>=edgex)
  36.         setPos(scene()->sceneRect().left(),pos().y());
  37.     if(pos().y()<=edgetop)
  38.         setPos(pos().x(),scene()->sceneRect().bottom());
  39.     if(pos().y()>=edgebottom)
  40.         setPos(pos().x(),scene()->sceneRect().top());

  41.     angle+=(qrand()%10)/20.0;
  42.     qreal dx=fabs(sin(angle*PI)*10.0);
  43.     qreal dy=(qrand()%200)-10.0;

  44.     setPos(mapToParent(dx,dy));
  45. }


复制代码
  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. #include "butterfly.h"
  4. #include <QGraphicsScene>

  5. int main(int argc, char *argv[])
  6. {
  7.     QApplication a(argc, argv);
  8.     QGraphicsScene *scene = new QGraphicsScene;
  9.     scene->setSceneRect(QRectF(-200,-200,400,400));

  10.     Butterfly *butterfly = new Butterfly;
  11.     butterfly->setPos(-100,0);

  12.     scene->addItem(butterfly);
  13.      
  14.     QGraphicsView *view = new QGraphicsView;
  15.    
  16.     view->setScene(scene);
  17.     view->resize(400,400);
  18.     view->show();

  19.     return a.exec();
  20. }
复制代码
这是一个实现蝴蝶飞舞效果的程序
第一个是头文件 第二个是类函数定义
第三个是主函数

我想问大家
函数 C/C++ code?
1
2
3
4
5
6
7
8
9
10
11
12
13
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;
    }
}




看它的定义 是实现图片变换的
可是在主函数里,找不到对它的调用

回复

使用道具 举报

累计签到:955 天
连续签到:1 天
2016-4-5 09:38:33 显示全部楼层
这个函数是继承过来的,需要在实现自己目的的时候重写。
回复

使用道具 举报

累计签到:12 天
连续签到:1 天
2016-5-17 20:36:03 显示全部楼层
是QT 内部在render 时自己调用的 ,不需要我们调用,你只需实现它就行了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了