Qt,QTransform旋转 [英] Qt, QTransform rotation

查看:4000
本文介绍了Qt,QTransform旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在圆柱体上绘制文字。这意味着,我有五行文本。顶行按X轴旋转10度。第二个为5度。中间不旋转。四行旋转-5度。五个旋转-10度。
Rows 1,2,3绘制好,但是4,5行出错了。我做错了什么?
我提供了一个理解问题和代码片段的图片:

I'm trying to draw text "on cylinder". It means, that I have five rows of text. Top row is rotated by the X axis for 10 degrees. The second for 5 degrees. The middle is not rotated at all. The four's row is rotated for -5 degrees. The five's is rotated for -10 degrees. Rows 1, 2, 3 draws OK, but something is wrong with 4,5 rows. What am I doing wrong ? I provides an image for understanding a problem and code snippet:

for( int i = 0; i < iterationsCount; ++i )
{
    const QRect r( x2, y2, textWidth, itemHeight );

    const QString text = sections.at( section ).values.at( index );

    int rsc = 0;


    p->save();

    rsc = widgetHeight / 2 - y;

    p->setTransform(QTransform().rotate(rsc, Qt::XAxis));


    if( type == Section::DaySectionShort ||
        type == Section::DaySectionLong )
    {


        QStringList values = text.split( QLatin1Char( ' ' ) );

        p->setPen(
            lighterColor( opt.palette.color( QPalette::WindowText ), 75 ) );
        p->drawText( r, Qt::AlignLeft | Qt::TextSingleLine, values.at( 0 ) );

        p->setPen( opt.palette.color( QPalette::WindowText ) );
        p->drawText( r, Qt::AlignLeft | Qt::TextSingleLine, values.at( 1 ) );
    }
    else
    {
       p->drawText( r, Qt::AlignLeft | Qt::TextSingleLine, text );
    }

    p->setTransform(QTransform().rotate(-rsc, Qt::XAxis));

    index = nextIndex( index, sections.at( section ).values.size() );
    y += itemHeight + itemTopMargin;

    p->restore();
}

我的问题

推荐答案

em>完成代码,再现问题,我不能猜到有什么错。但最可能的原因是不正确的 rsc 计算。至少以下草稿工作:

As you have not provided a minimal complete code that reproduces the problem, I cannot guess what is wrong there. But the most probable reason is incorrect rsc calculation. At least the following draft works:

#include <QtCore>
#include <QtGui>
#include <QtWidgets>

class MyWidget: public QWidget
{
public:
    MyWidget(QWidget *parent = nullptr)
        : QWidget(parent)
    {
        QFont f = font();
        f.setPointSize(15);
        setFont(f);
    }

protected:
    void paintEvent(QPaintEvent *event) override
    {
        QWidget::paintEvent(event);
        QPainter p(this);

        const int itemHeight = fontMetrics().height();
        const int itemTopMargin = 15;
        const int xOffset = 15;
        int y = itemHeight;

        for (size_t i = 0; i < 5; ++i) {
            // The angle is in range [-40, 40]. Remove " * 4" for [-10, 10].
            const int rsc = (10 - 5 * i) * 4;
            qDebug() << i << ":\t" << rsc << "\t" << y;

            /*
                Rotation must be performed relative to central point of the
                drawn item. Transformations below are applied in reverse order.
                At first translate item to make it's center in (0, 0). At
                second rotate it relative to X axis. At third move the item to
                desired position.
            */
            QTransform transform;
            transform.translate(xOffset, y + itemHeight / 2);
            transform.rotate(rsc, Qt::XAxis);
            transform.translate(0, - itemHeight / 2);
            p.setTransform(transform);

            p.drawText(QPoint(), QString("(Item no. %1)").arg(i + 1));

            y += itemHeight + itemTopMargin;
        }
    }
};

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    MyWidget widget;
    widget.setMaximumSize(200, 250);
    widget.show();

    return app.exec();
}

这里使用的转换是复杂的,因为需要旋转每个项目中心 y ,而不是 y = 0

The transformation used here is complicated because of need to rotate each item relative to it's central y, not y = 0. This also may be the case.

字体和角度增加,以更好地看待考虑的效果。

Font and angles are increased to see considered effects better.

这篇关于Qt,QTransform旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆