首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Qt5绘图转换()、旋转()、字体填充问题

Qt5绘图转换()、旋转()、字体填充问题
EN

Stack Overflow用户
提问于 2022-11-11 16:53:29
回答 2查看 42关注 0票数 -1

我正在写我的第一个QT5应用程序..。这使用第三方地图库(QGeoView)。我需要在这张地图上画一个物体(就像一架风格化的飞机)。遵循库编码准则,我从基类QGVDrawItem my QGVAirplane派生。飞机类包含航向和位置值:这些值必须用于在地图上绘制飞机(当然,在正确的位置和正确的航向)。库要求QGVDrawItem导数覆盖三个基类方法:

代码语言:javascript
运行
复制
QPainterPath projShape() const;
void projPaint(QPainter* painter);
void onProjection(QGVMap* geoMap)

第一种方法用于实现需要更新的地图区域,第二种方法负责在地图上绘制对象。第三种方法需要从地图上的坐标空间重新投影点(它与解决我的问题无关)。

我的代码是这样的:

代码语言:javascript
运行
复制
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上正确地指向--我多次尝试翻译边界矩形,以便在没有运气的情况下对对象进行居中。

另一个小问题是字体的填充。飞机字形下的标签不是实心的,而是与飞机的颜色相同的。

有谁可以帮我?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-15 11:37:50

我终于实现了我的意思.

代码语言:javascript
运行
复制
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填充。

票数 0
EN

Stack Overflow用户

发布于 2022-11-12 02:33:55

一般说来,旋转的一般模式,关于原点的比例,然后以你最后的翻译结束。

下面是伪代码,但说明在进行任何旋转或缩放之前,需要将对象的原点转换为(0,0)。在完成旋转和缩放之后,可以将对象从(0,0)移回原来的位置。从这里开始,任何翻译后的步骤都可以应用.

代码语言:javascript
运行
复制
translate( -origin.x, -origin.y );
rotate( angle );
scale( scale.x, scale y);
translate(  origin.x,  origin.y );
translate(  translation.x, translation.y )
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74405775

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档