c++ - QT - 拖动时的像素图人工制品

标签 c++ qt

我对为拖动事件创建的像素图有疑问。对于派生的 QGraphicsRectItem 的拖动事件,我从该项目创建了一个半透明的像素图。

在调试版本中一切看起来都很好。

enter image description here

但在发布版本中,拖动像素图有一些周期性和随机性的人工制品

enter image description here

代码如下:

QPixmap MyGraphicsRectItem::toPixmap() const
{
   QRect r = boundingRect().toRect();
   QPixmap pixmap(r.width(), r.height());
   QColor dragColor(color);
   dragColor.setAlphaF(0.5);

   QPainter painter;
   painter.begin(&pixmap);

   painter.fillRect(pixmap.rect(), dragColor);
   painter.setPen(Qt::white);

   QFont font("SegoeUI");
   font.setBold(true);

   painter.setFont(font);
   painter.drawText(pixmap.rect(), QString(" ") + textItem->toPlainText());

   if (pixItem != nullptr) {
       painter.setOpacity(0.5);
       painter.drawPixmap(pixItem->pos(), pixItem->pixmap());
   }

   painter.end();

   return pixmap;
}

这会不会是一种内存问题?

最佳答案

QPixmap 使用未初始化的数据进行初始化。在 Debug 中,这通常设置为固定模式,但在 Release 中它是垃圾。

使用前应先用透明色填充像素图。

QPixmap::QPixmap(int width, int height)

Constructs a pixmap with the given width and height. If either width or height is zero, a null pixmap is constructed.

Warning: This will create a QPixmap with uninitialized data. Call fill() to fill the pixmap with an appropriate color before drawing onto it with QPainter.

(来自Qt Docs)

关于c++ - QT - 拖动时的像素图人工制品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45982065/

相关文章:

c++ - 样式 : Selection color on editable QComboBox

qt - 使用特定 QT 版本运行二进制文件 - 不能混合不兼容的 Qt 库

c++ - OpenCV + CUDA + OSX 小牛队

c++ - 非常量引用绑定(bind)到临时的 Visual Studio 错误?

c++ - 我的REST API客户端二维码或设置有什么问题

c++ - 用 go + swig 替换 c++

C++无锁线程同步

c++ - 如何将大量数据存储为 std::map 中的值?

c++ - 设置每个样式表的 QStatusBar 最小高度

android - 如何使用 Qt 5.6 让 NFC 在 Android 上运行