Qt QGraphicsItem 双缓冲绘图

在.h里面定义

QImage *image;//先画到image上面,然后再贴上去    
bool painterok = false;//判断是否是第一次绘图,第一次绘图完毕,下次painterok为true,不在绘图,只是贴image;

.cpp

重载paint函数

void RoboPointItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{	
//	Q_UNUSED(widget) 
   Q_UNUSED(option);  
​​​​​​​  if(painterok)//判断 boundingRect是你自己规定的大小
    {
        painter->setRenderHint(QPainter::Antialiasing, true);
        painter->drawImage(static_cast<int>( boundingRect().x()),
                            static_cast<int>( boundingRect().y()),*image);
        return;
    }

   //你的第一次绘图的内容 begin
    QPainter paint(image);
    paint.setRenderHint(QPainter::Antialiasing, true);
    paint.setPen(pen);
    foreach (auto item,MapAllPoint)
    {
        paint.drawPoint(QPointF(item.x,item.y));
    }
    paint.setPen(MapPen);
    paint.setBrush(MapBrush);
    paint.drawRect(BoundingRect.adjusted(-20, -20, 20, 20));
    paint.fillRect(BoundingRect.adjusted(-20, -20, 20, 20), QColor(15, 15, 15, 35));
    //你的第一次绘图内容 end
  //  image->save("a.png");
//    painter->drawPixmap(static_cast<int>( boundingRect().x()),
//                        static_cast<int>( boundingRect().y()),*pixmap);
    painterok = true;
}

注意几个坑:

1:可能回一直报 Painter not active

这个时候看看你的image在构造函数定义的时候有给初始值的大小吗?类是这样

image = new QImage(static_cast<int>(boundingRect().width()),static_cast<int>(boundingRect().height()),                       QImage::Format::Format_RGB32);

2:还有我为啥在painter里面又定义了一个paint,因为之前我直接用参数列表里面的painter去画在image上面,不管是用begin(),end()函数,还是其他的都不行,第一次画的好好的,第二次就不显示了,后来我保存了image,一看,根本就没画上去。后来才想着定义一个画好了之后,再用参数里面的painter去贴image.

3.这种直接在paint函数里面进行双缓冲绘制的,前提是你的这个对象是临时变量,用的时候就会new,然后给新的数据,不然如果是全局的变量,那就不行了,只能采用放在外面写,需要刷新的时候先调用那个函数,将新的数据画上去,然后再update();就好了

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要复制一个 QGraphicsItem,你需要实现 QGraphicsItem 的 `QGraphicsItem::clone()` 方法。这个方法会返回一个新的 QGraphicsItem 实例,该实例是原来 QGraphicsItem 的一个副本。在实现 `clone()` 方法时,你需要注意以下几点: 1. 你需要在你的 QGraphicsItem 子类中重新实现 `clone()` 方法。 2. `clone()` 方法需要返回一个新的 QGraphicsItem 实例。 3. 在 `clone()` 方法中,你需要创建一个新的 QGraphicsItem 实例,并将原始 QGraphicsItem 的属性复制到它上面。 以下是一个示例: ```cpp class MyItem : public QGraphicsItem { public: MyItem(QGraphicsItem *parent = nullptr); // 实现 clone 方法 virtual MyItem *clone() const; protected: // 实现 paint 方法 virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; // 实现 boundingRect 方法 virtual QRectF boundingRect() const override; private: // 添加其他成员变量和函数 }; MyItem::MyItem(QGraphicsItem *parent) : QGraphicsItem(parent) { // 初始化 MyItem 的其他成员变量 } MyItem *MyItem::clone() const { MyItem *newItem = new MyItem(); // 复制 MyItem 的属性到 newItem 上 // ... return newItem; } void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { // 实现 MyItem 的绘制逻辑 } QRectF MyItem::boundingRect() const { // 返回 MyItem 的边界矩形 } ``` 在使用 `clone()` 方法时,你可以像下面这样使用: ```cpp MyItem *item1 = new MyItem(); MyItem *item2 = item1->clone(); ``` 这样,`item2` 就是 `item1` 的一个副本了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搁浅的渔

创作不易,多多支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值