当前位置:   article > 正文

[问题记录]Qt使用QPainter在QImage、QBitmap、QPixmap上面绘图时出现杂色_在qimage上绘图

在qimage上绘图

目录

1. 问题现象

2. 问题原因

3. 解决方案


1. 问题现象

使用QPainter,在QImage上绘图,能明显看到顶部有杂色,在QBitmap上则更明显,唯一在QPicture上绘图没出现该问题。

代码

  1. void Widget::paintImage()
  2. {
  3. QPainter painter;
  4. QImage qImage(100, 100, QImage::Format_ARGB32);
  5. painter.begin(&qImage);
  6. painter.setBrush(Qt::yellow);
  7. painter.setPen(QPen(Qt::green, 1));
  8. painter.drawRect(10, 10, 60, 60);
  9. painter.drawText(10, 10, 60, 60, Qt::AlignCenter, tr("QImage"));
  10. painter.setBrush(QColor(0, 0, 0, 100));
  11. painter.drawRect(50, 50, 40, 40);
  12. painter.end();
  13. QPixmap qPixmap(100, 100);
  14. painter.begin(&qPixmap);
  15. painter.setPen(QPen(Qt::green, 1));
  16. painter.setBrush(Qt::yellow);
  17. painter.drawRect(10, 10, 60, 60);
  18. painter.drawText(10, 10, 60, 60, Qt::AlignCenter, tr("QQPixmapImage"));
  19. painter.setBrush(QColor(0, 0, 0, 100));
  20. painter.drawRect(50, 50, 40, 40);
  21. painter.end();
  22. QBitmap qBitmap(100, 100);
  23. painter.begin(&qBitmap);
  24. painter.setPen(QPen(Qt::green, 1));
  25. painter.setBrush(Qt::yellow);
  26. painter.drawRect(10, 10, 60, 60);
  27. painter.drawText(10, 10, 60, 60, Qt::AlignCenter, tr("QBitmap"));
  28. painter.setBrush(QColor(0, 0, 0, 100));
  29. painter.drawRect(50, 50, 40, 40);
  30. painter.end();
  31. QPicture qPicture;
  32. painter.begin(&qPicture);
  33. painter.setPen(QPen(Qt::green, 1));
  34. painter.setBrush(Qt::yellow);
  35. painter.drawRect(10, 10, 60, 60);
  36. painter.drawText(10, 10, 60, 60, Qt::AlignCenter, tr("QPicture"));
  37. painter.setBrush(QColor(0, 0, 0, 100));
  38. painter.drawRect(50, 50, 40, 40);
  39. painter.end();
  40. painter.begin(this);
  41. painter.drawImage(50, 20, qImage);
  42. painter.drawPixmap(200, 20, qPixmap);
  43. painter.drawPixmap(50, 170, qBitmap);
  44. painter.drawPicture(200, 170, qPicture);
  45. }

2. 问题原因

只能是推测,未证实

QImage、QPixmap、QBitmap主要作用还是文件,文件初始化时,如果没有填充背景,那初始的像素就是不确定的,即显示的杂色。

也可能是和电脑显示驱动关联,使用到显示驱动初始化的图片,驱动不兼容等导致的杂色。

3. 解决方案

在创建相关对象后,先使用fill填充背景,再绘图

  1. void Widget::paintImage()
  2. {
  3. QPainter painter;
  4. QImage qImage(100, 100, QImage::Format_ARGB32);
  5. qImage.fill(QColor(0, 0, 0, 0));
  6. ...
  7. QPixmap qPixmap(100, 100);
  8. qPixmap.fill();
  9. ...
  10. QBitmap qBitmap(100, 100);
  11. qBitmap.fill();
  12. ...
  13. }

效果如下

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/746475
推荐阅读
相关标签
  

闽ICP备14008679号