我正在写我的第一个QT5应用程序..。这使用第三方地图库(QGeoView)。我需要在这张地图上画一个物体(就像一架风格化的飞机)。遵循库编码准则,我从基类QGVDrawItem
my QGVAirplane
派生。飞机类包含航向和位置值:这些值必须用于在地图上绘制飞机(当然,在正确的位置和正确的航向)。库要求QGVDrawItem
导数覆盖三个基类方法:
QPainterPath projShape() const;
void projPaint(QPainter* painter);
void onProjection(QGVMap* geoMap)
第一种方法用于实现需要更新的地图区域,第二种方法负责在地图上绘制对象。第三种方法需要从地图上的坐标空间重新投影点(它与解决我的问题无关)。
我的代码是这样的:
void onProjection(QGVMap* geoMap)
{
QGVDrawItem::onProjection(geoMap);
mProjPoint = geoMap->getProjection()->geoToProj(mPoint);
}
QPainterPath projShape() const
{
QRectF _bounding = createGlyph().boundingRect();
double _size = fmax(_bounding.height(), _bounding.width());
QPainterPath _bounding_path;
_bounding_path.addRect(0,0,_size,_size);
_bounding_path.translate(mProjPoint.x(), mProjPoint.y());
return _bounding_path;
}
// This function creates the path containing the airplane glyph
// along with its label
QPainterPath createGlyph() const
{
QPainterPath _path;
QPolygon _glyph = QPolygon();
_glyph << QPoint(0,6) << QPoint(0,8) << QPoint(14,6) << QPoint(28,8) << QPoint(28,6) << QPoint(14,0);
_path.addPolygon(_glyph);
_path.setFillRule(Qt::FillRule::OddEvenFill);
_path.addText(OFF_X_TEXT, OFF_Y_TEXT, mFont , QString::number(mId));
QTransform _transform;
_transform.rotate(mHeading);
return _transform.map(_path);
}
// This function is the actual painting method
void drawGlyph(QPainter* painter)
{
painter->setRenderHints(QPainter::Antialiasing, true);
painter->setBrush(QBrush(mColor));
painter->setPen(QPen(QBrush(Qt::black), 1));
QPainterPath _path = createGlyph();
painter->translate(mProjPoint.x(), mProjPoint.y());
painter->drawPath(_path);
}
当然:
mProjPoint
是飞机的位置,mHeading
是航向(飞机指向的方向),mId
是识别飞机的数字(将显示为飞机字形下的标签),mColor
是分配给飞机的颜色。这里的问题是旋转和平移转换的混合:由于对象是旋转的,projShape()
方法返回一个边界矩形,该矩形没有完全重叠在地图上绘制的对象.我还怀疑对象的中心没有在mProjPoint
上正确地指向--我多次尝试翻译边界矩形,以便在没有运气的情况下对对象进行居中。
另一个小问题是字体的填充。飞机字形下的标签不是实心的,而是与飞机的颜色相同的。
有谁可以帮我?
发布于 2022-11-15 11:37:50
我终于实现了我的意思.
QPainterPath projShape() const
{
QPainterPath _path;
QRectF _glyph_bounds = _path.boundingRect();
QPainterPath _textpath;
_textpath.addText(0, 0, mFont, QString::number(mId));
QRectF _text_bounds = _textpath.boundingRect();
_textpath.translate(_glyph_bounds.width()/2-_text_bounds.width()/2, _glyph_bounds.height()+_text_bounds.height());
_path.addPath(_textpath);
QTransform _transform;
_transform.translate(mProjPoint.x(),mProjPoint.y());
_transform.rotate(360-mHeading);
_transform.translate(-_path.boundingRect().width()/2, -_path.boundingRect().height()/2);
return _transform.map(_path);
}
void projPaint(QPainter* painter)
{
painter->setRenderHint(QPainter::Antialiasing, true);
painter->setRenderHint(QPainter::TextAntialiasing, true);
painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
painter->setRenderHint(QPainter::HighQualityAntialiasing, true);
painter->setBrush(QBrush(mColor));
painter->setPen(QPen(QBrush(Qt::black), 1));
painter->setFont(mFont);
QPainterPath _path = projShape();
painter->drawPath(_path);
}
不幸的是,我仍然遭受了文字填充模式的小问题:我希望文本有一个坚实的黑色填充,而不是我用于字形/多边形的mColor
填充。
发布于 2022-11-12 02:33:55
一般说来,旋转的一般模式,关于原点的比例,然后以你最后的翻译结束。
下面是伪代码,但说明在进行任何旋转或缩放之前,需要将对象的原点转换为(0,0)。在完成旋转和缩放之后,可以将对象从(0,0)移回原来的位置。从这里开始,任何翻译后的步骤都可以应用.
translate( -origin.x, -origin.y );
rotate( angle );
scale( scale.x, scale y);
translate( origin.x, origin.y );
translate( translation.x, translation.y )
https://stackoverflow.com/questions/74405775
复制相似问题