【问题标题】:Qt Rotating text around its center pointQt围绕其中心点旋转文本
【发布时间】:2016-10-28 17:01:00
【问题描述】:

我正在使用 Qt 5.6,我想在一个圆圈周围绘制一些文本标签并旋转文本标签以根据其围绕圆圈的位置来定位文本,所以 12 点钟会有 0 度旋转, 3 点钟旋转 90 度,6 点钟旋转 180 度等。

我正在围绕其中心位置对齐文本:

    void drawText(QPainter* pobjPainter, qreal fltX, qreal fltY
                  ,int intFlags, const QString* pobjText) {
        const qreal fltSize = 32767.0;

        QPointF ptfCorner(fltX, fltY - fltSize);

        if ( (intFlags & Qt::AlignHCenter) == Qt::AlignHCenter ) {
            ptfCorner.rx() -= fltSize / 2.0;
        } else if ( (intFlags & Qt::AlignRight) == Qt::AlignRight ) {
            ptfCorner.rx() -= fltSize;
        }
        if ( (intFlags & Qt::AlignVCenter) == Qt::AlignVCenter ) {
            ptfCorner.ry() += fltSize / 2.0;
        } else if ( (intFlags & Qt::AlignTop) == Qt::AlignTop ) {
            ptfCorner.ry() += fltSize;
        }
        QRectF rctPos(ptfCorner, QSizeF(fltSize, fltSize));
        pobjPainter->drawText(rctPos, intFlags, *pobjText);
    }

我想对文本应用旋转。

我想重现类似于所示内容的内容:

http://www.informit.com/articles/article.aspx?p=1405545&seqNum=2

似乎rotate函数旋转了整个painter画布,所以坐标必须考虑到旋转,这真的让我很难过。我想将文本定位在椭圆周围然后旋转它,我怎么知道坐标应该是什么?

【问题讨论】:

  • this answer 对您的情况没有帮助吗?
  • 很有趣,我一直在看这个,但是试过了,它对我不起作用......我还在调查。

标签: c++ qt


【解决方案1】:

坚持使用时钟示例,您可以尝试类似...

virtual void paintEvent (QPaintEvent *event) override
  {
    QPainter painter(this);
    double radius = std::min(width(), height()) / 3;
    for (int i = 0; i < 12; ++i) {
      int numeral = i + 1;
      double radians = numeral * 2.0 * 3.141592654 / 12;

      /*
       * Calculate the position of the text centre as it would be required
       * in the absence of a transform.
       */
      QPoint pos = rect().center() + QPoint(radius * std::sin(radians), -radius * std::cos(radians));

      /*
       * Set up the transform.
       */
      QTransform t;
      t.translate(pos.x(), pos.y());
      t.rotateRadians(radians);
      painter.setTransform(t);

      /*
       * Specify a huge bounding rectangle centred at the origin.  The
       * transform should take care of position and orientation.
       */
      painter.drawText(QRect(-(INT_MAX / 2), -(INT_MAX / 2), INT_MAX, INT_MAX), Qt::AlignCenter, QString("%1").arg(numeral));
    }
  }

【讨论】:

  • 谢谢,现在我该如何调整它以适应 0 到 360 度的范围,其中标签可以位于面部从 0 到 360 度的任何位置?
  • 不太确定我是否遵循。您知道圆心的坐标(我在示例中使用了rect().centre()),它的半径和基于值的垂直顺时针角度(在您的情况下为 0 --> 360)。一切都应该遵循这些基本参数。对不起,如果我误解了。如果你能更具体一点,我很乐意更新答案。
猜你喜欢
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 2012-01-17
  • 2013-05-19
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多