I am having problem where my Qt program crashes after calling QPainter::drawPixmap()
. I have spent 2 days debugging this and have decided that I must be unintentionally abusing some feature of Qt.
A working example of this problem can be found here.
My code consists of a QML file that updates the following properteries:
Q_PROPERTY(qreal longitude READ getLongitude WRITE setLongitude NOTIFY latitudeChanged)
Q_PROPERTY(qreal latitude READ getLatitude WRITE setLatitude NOTIFY latitudeChanged)
void Map::setLongitude(qreal longitude)
{
double diff = (this->longitude - this->pixmapCenter.longitude()) * scale;
this->longitude = longitude;
if (qFabs(diff) > 50)
{
MapTile result = updatePixmap(scale, longitude, latitude);
pixmap = result.pixmap;
pixmapCenter = result.center;
}
update();
}
void Map::setLatitude(qreal latitude)
{
this->latitude = latitude;
}
That in turn regenerates a new Pixmap
MapTile updatePixmap(double scale, double longitude, double latitude)
{
QPixmap myPixmap(800, 400);
myPixmap.fill(Qt::transparent);
QPainter painter(&myPixmap);
painter.translate(400, 240);
QPen pen;
pen.setColor(Qt::white);
pen.setWidth(1);
painter.setPen(pen);
QRectF boundaries(QPointF(-91.55 , 41.55) * scale,
QPointF(-91.45, 41.45) * scale);
boundaries.translate(-longitude * scale, -latitude * scale);
painter.drawRect(boundaries);
painter.end();
QGeoCoordinate center(latitude, longitude);
return MapTile(myPixmap, center);
}
This new pixmap is then drawn on the screen at the appropriate location. It is important to note that the program runs fine for a few seconds before it crashes.
It crashes with a segfault error in qdrawhelper_sse2.cpp line 587.
void Map::paint(QPainter *painter)
{
painter->translate(boundingRect().width()/2, boundingRect().height()/2);
painter->scale(1, -1);
painter->translate((pixmapCenter.longitude() - longitude) * scale,
(pixmapCenter.latitude() - latitude) * scale);
QPoint corner(-pixmap.width()/2, -pixmap.height()/2);
painter->drawPixmap(corner, this->pixmap);
}
Here is an image of the moment of the crash
This is a bug in Qt 5.9 and 5.10. See here.