【发布时间】:2018-04-08 09:47:54
【问题描述】:
我在 QPixmap 上使用 QPainter 时遇到了内存泄漏问题。我需要在地图(导航点)上围绕地球设置大约 250000 个点。它们中的每一个都共享相同的图标,但具有特定的标签。我将所有这些点添加到同一层。
这是我的代码:
void Gspv::addFixes() // waypoints layer
{
waypoints = new GeometryLayer("Waypoints", mapadapter);
mc->addLayer(waypoints);
//Icon
QPixmap icone(38,38);
icone.fill(Qt::transparent);
QPainter *paint = new QPainter(&icone);
paint->setRenderHint(QPainter::Antialiasing);
paint->setBrush(Qt::cyan);
paint->setPen(Qt::black);
static const QPoint triangle[3] = {
QPoint(15,0),
QPoint(3, 20),
QPoint(27,20)
};
paint->drawPolygon(triangle, 3);
delete paint;
//Check file
QFile file(QCoreApplication::applicationDirPath() + "/data/Waypoints.txt");
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::information(0, "erreur lecture fichier : " + file.fileName(), file.errorString());
return;
}
//Parsing file
QTextStream in(&file);
while(!in.atEnd()) {
QString line = in.readLine();
QStringList fields = line.split(",");
QString str = fields.at(1);
double latitude = str.toDouble();
str = fields.at(2);
double longitude = str.toDouble();
str = fields.at(0);
addCode(icone,str); // Prob here
//Add current point to layer
Point* pointCourant = new Point(longitude, latitude, icone, str);
pointCourant->setBaselevel(10);
pointCourant->setMaxsize(QSize(38, 38));
waypoints->addGeometry(pointCourant);
}
file.close();
}
//Add code to QPixmap
void Gspv::addCode(QPixmap &pm, QString &code)
{
QPainter pmp(&pm);
pmp.setPen(Qt::black);
pmp.setFont(QFont("ArialBold",9));
pmp.eraseRect(0,20,38,15);
pmp.drawText(0,32,code);
}
一切都按预期工作,除了它会导致严重的内存泄漏。 问题是在 while 循环中添加代码时。无论我做什么(在 addfixes 方法中添加代码或在特定的 addCode 方法中添加代码),我都会遇到内存泄漏。
即使代码只简化为:
void Gspv::addCode(QPixmap &pm, QString &code)
{
QPainter pmp(&pm);
// here it does nothing !
}
内存泄漏是。而且无论语句是静态的还是动态的,结果都是一样的。
不加代码,内存使用量在152 Mo左右,已经够低了。添加这个简单的代码后,它会出现内存不足的问题。
我看了很多关于 QPainter 和内存泄漏的帖子,但我无法处理。
你会为此提供帮助吗?
提前致谢。
【问题讨论】:
-
使用 QImage 代替 QPixmap。
-
你确定画家是泄漏的源头吗?您确定创建者会构建并执行您最近的代码吗?它经常卡住......
-
我会尝试用实际答案回复,但它已经被覆盖了,而不是一次:stackoverflow.com/questions/707492/how-do-i-paint-with-qpainter 您的代码应该超载
paintEvent并请求update。以这种方式重构它,将一个对象与一些任务分开,供paintEvent绘制。 -
关于泄漏:只是不清楚
waypoints。其余的代码只是没有做应该做的事情。
标签: qt memory-leaks qpainter