QPainter Pro


QPainter

  • 三要素
    • 画在什么设备上? pixmap
    • 用什么画在什么上? painter => pixmap
    • 渲染在什么部件上(render)? QGraphicsView QGraphicsScene

QGraphicsView

  • 显示

_view = new QGraphicsView(this);

QGraphicsScene

  • 现场 添加到显示里

_view->setScene(_scene = new QGraphicsScene);

  • 在现场中添加 item
  • 修改 item 的样式

_scene->addItem(lineitem = new QGraphicsLineItem(0,0,100,100));
_scene->addItem(textitem = new QGraphicsTextItem("ohayo"));
_scene->addItem(pixmapItem = new QGraphicsPixmapItem(QPixmap("../xxxx.png")));

textitem->setPos(QPointF(200, 300));
textitem->setFont(QFont("aaa",50,20,1));

pixmapItem->setPos(100,100);

保存图片

  • 先渲染图片
    • 三要素: 设备 画笔 渲染 ==> 保存

// _view->render() //重新操作一遍
// _scene->render()

    QPixmap pixmap(size()); //  先要有一个画的设备
    QPainter painter(&pixmap);  //  画的工具

    painter.fillRect(QRect(0,0,size().width(),size().height()),Qt::white);  //  画板背景
    _scene->render(&painter);   //  渲染  用什么渲染(画笔) 在什么上渲染(pixmap)

    pixmap.save("../abb.png");

QGraphicsItemAnimation 动画

  • 加入 item ==> 设置QTimeline ==> 循环次数 ==>

QGraphicsItemAnimation * animation = new QGraphicsItemAnimation;

animation->setItem(pixmapItem);

QTimeLine* timeline = new QTimeLine(3000);  // ms
timeline->setLoopCount(2);

animation->setTimeLine( timeline );
animation->setTranslationAt(1,200,200);
timeline->start();

定时器


timer = new QTimer();
timer->setInterval(1000);   // 间隔1s
// 超时做的事 connect
connect(timer,
        &QTimer::timeout,
        this,
        &MyWidget::sl_timeout);
timer->start();

    //  只触发一次
timer->singleShot(1000,this,&MyWidget::sl_timeout);


文章作者: bySouffle
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 bySouffle !
  目录