请教大家QGraphicsItem 类的paint函数

XwinterwinterwinterX 2016-04-02 08:28:32
#ifndef BUTTERFLY_H
#define BUTTERFLY_H

#include <QObject>
#include <QGraphicsItem>
#include <QPainter>
#include <QGraphicsScene>
#include <QGraphicsView>

class Butterfly : public QObject,public QGraphicsItem
{
Q_OBJECT
public:
explicit Butterfly(QObject *parent = 0);
void timerEvent(QTimerEvent *);
QRectF boundingRect() const;

signals:

public slots:
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);

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

qreal angle;
};

#endif // BUTTERFLY_H


#include "butterfly.h"
#include <math.h>

const static double PI=3.1416;

Butterfly::Butterfly(QObject *parent) :
QObject(parent)
{
up = true;
pix_up.load("D://CH7/CH701/Butterfly/up.jpg");
pix_down.load("D://CH7/CH701/Butterfly/down.jpg");

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()+adjust*2);
}

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 *)
{
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()%200)-10.0;

setPos(mapToParent(dx,dy));
}




#include "mainwindow.h"
#include <QApplication>
#include "butterfly.h"
#include <QGraphicsScene>

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

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

scene->addItem(butterfly);

QGraphicsView *view = new QGraphicsView;

view->setScene(scene);
view->resize(400,400);
view->show();

return a.exec();
}



这是一个实现蝴蝶飞舞效果的程序
第一个是头文件 第二个是类函数定义
第三个是主函数

我想问大家
函数
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;
}
}

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


...全文
1517 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hx_snake 2017-08-31
  • 打赏
  • 举报
回复
引用 7 楼 BigPanda_CPU 的回复:
请问这个paint是在setPos后,调用的是吧
应该是scene->additem后就调用了,然后一直循环
BigPanda_CPU 2017-08-30
  • 打赏
  • 举报
回复
请问这个paint是在setPos后,调用的是吧
孔方发发 2017-05-02
  • 打赏
  • 举报
回复

void classname::paintEvent(QPaintEvent *)
{
        drawHMSClock(this,460,350);
}
/**************************************************************************************************
**  函数名称: drawHMSClock
**  功能描述:
**  输入参数:
**  输出参数:
**  返回参数:
**************************************************************************************************/
void drawHMSClock(QPaintDevice *pd,int x,int y)
{
    //setMinimumSize(500,500);
    QPainter painter(pd);

    painter.setRenderHint(QPainter::Antialiasing); //反走样--消除走样

    QPen pen8;
    pen8.setColor(Qt::black);
    pen8.setWidth(8);
    pen8.setStyle(Qt::SolidLine);
    painter.setPen(pen8);//使用画笔
    painter.setBrush(QColor(0,0,0,255));
    painter.drawEllipse(x-200,y-200,400,400);//画黑外圈

    //勾画白色背景
    painter.setPen(Qt::white);
    painter.setBrush(Qt::white);
    painter.drawEllipse(x-200,y-200,400,400);

    painter.translate(x,y); //将点(x,y)设为原点

    QPen pen1;
    pen1.setWidth(1);
    QPen pen3;
    pen3.setWidth(3);

    painter.save(); //保存坐标系状态
    for(int i=0;i<60;i++)//绘制表盘刻度
    {
        if(i%5!=0)//绘制短刻度线
        {
            painter.setPen(pen1);
            painter.drawLine(200,0,190,0);
        }
        else//绘制长刻度线
        {
            painter.setPen(pen3);
            painter.drawLine(200,0,185,0);
        }
        painter.rotate(6.0);
    }
    painter.restore(); //恢复以前的坐标系状态
    //绘制文字
    QFont font("宋体",16,QFont::Bold);//设置字体样式
    //使用字体
    painter.setFont(font);
    painter.setPen(Qt::black);
    painter.drawText(165,8,QObject::tr("3"));
    painter.drawText(-175,8,QObject::tr("9"));
    painter.drawText(-13,-160,QObject::tr("12"));
    painter.drawText(-5,175,QObject::tr("6"));


    QTime time = QTime::currentTime();
    //根据个点坐标画针分针秒针
    QPoint hourHand[5] = {QPoint(-3,10),QPoint(3,10),QPoint(3,-145),QPoint(0,-160),QPoint(-3,-145) };

    QPoint minuteHand[2] = {QPoint(0,20),QPoint(0,-170)};

    QPoint secondHand[2] = {QPoint(0,30),QPoint(0,-190)};

    //绘制时针
    QPen penh;
    penh.setWidthF(1);
    painter.setPen(penh);

    painter.setBrush(Qt::black);
    painter.save(); //保存坐标系状态
    painter.rotate(30.0 * time.hour() + (qreal)time.minute() / 60.0 * 30.0 );//坐标轴旋转的角度
    painter.drawConvexPolygon(hourHand,5);//根据三个坐标点绘图
    painter.restore(); //恢复以前的坐标系状态

    //绘制分针
    QPen penm;
    penm.setColor(Qt::green);
    penm.setWidthF(2);
    painter.setPen(penm);
    painter.save(); //保存坐标系状态
    painter.rotate(6.0 * time.minute() +(qreal)time.second() * 6.0 / 60.0);
    painter.drawConvexPolygon(minuteHand, 2); //绘制多边形
    painter.restore(); //恢复以前的坐标系状态

    //绘制秒针
    QPen pens;
    pens.setColor(Qt::red);
    pens.setWidthF(1);
    painter.setPen(pens);
    painter.setBrush(Qt::red);
    painter.save(); //保存坐标系状态
    painter.rotate(6.0*time.second());
    painter.drawConvexPolygon(secondHand,2);


    //绘制秒针上的两个红点
    painter.drawEllipse(-5,15,10,10);
    painter.drawEllipse(-3,-75,6,6);
    painter.restore(); //恢复以前的坐标系状态
   //绘制中心黑点
    painter.save(); //保存坐标系状态
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawEllipse(-6,-6,12,12);
    painter.restore(); //恢复以前的坐标系状态

}
pinren8284 2017-04-20
  • 打赏
  • 举报
回复
同问,而且不懂paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)和paintEvent ( QPaintEvent * )的异同或关系。QT新手请教各位,谢谢
CBCU 2017-03-26
  • 打赏
  • 举报
回复
引用 2 楼 u013466477 的回复:
void Butterfly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 这个只是一个绘制接口,虚的。 你要绘制什么,直接重写这个函数,再这里面画就行了
如果我要在QGraphicsItem中绘制一个QLabel+QSpinBox,我大概需要怎样去写paint?谢谢
东莞某某某 2016-04-06
  • 打赏
  • 举报
回复
跟paintEvent() 类似
冷静忍耐 2016-04-05
  • 打赏
  • 举报
回复
void Butterfly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) 这个只是一个绘制接口,虚的。 你要绘制什么,直接重写这个函数,再这里面画就行了
kyitnetking 2016-04-03
  • 打赏
  • 举报
回复
scene->addItem(butterfly); 大概在这个内部自动调用。这跟Qt的视图框架的渲染机制设计有关。如果要弄清楚就得看源代码,一般大家直接使用的就是这种框架的接口。

16,202

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧