QGraphicsView:蛇线和直线动画

发布于 2024-12-04 07:11:30 字数 578 浏览 1 评论 0原文

我有两个关于 QGraphicsView 框架的问题。

第一个问题是关于一条特殊的线:我想问是否可以画一条蛇线或波浪线(如链接图像中所示)?我将不胜感激任何解决方案,因为我不知道如何获得窦形线。

图像:一条蛇线

我的第二个问题是关于QGraphicsItemAnimation- 我想要为 QGraphicsLineItem 制作动画。现在我正在使用 QGraphicsItemAnimation 中的方法 setPosAt() 。问题是孔线移动了。我的目标是使用QGraphicsItemAnimation仅移动QGraphicsLineItem起点或终点。所以我想问一下这是否可能,或者是否有其他方法可以做到这样的事情?

提前致谢!

亲切的问候 - 菲伽玛

I have two questions regarding the QGraphicsView Framework.

The first question is regarding a special line: I wanted to ask whether it is possible to draw an Snake- or Wave-Line (like in the linked image)? I would be grateful for any solution, because I dont have any idea how to get the sinus-shaped line.

Image: a snake line

My second question is about the QGraphicsItemAnimation- I want to animate a QGraphicsLineItem. Right now I am using the method setPosAt() from the QGraphicsItemAnimation. The problem is, that the hole line is moved. My aim is to move only the start or the end point of the QGraphicsLineItem with the QGraphicsItemAnimation. So I want to ask whether this is possible or whether there is any other way to do something like this?

Thanks in advance!

Kind regards
- PhiGamma

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红颜悴 2024-12-11 07:11:30

对于你问题的第一部分:可能有几种方法

:使用 QPainterPathcubicTo() 方法创建“路径”,然后将 QGraphicsPath 与您创建的路径一起使用。为此,您需要使用贝塞尔曲线来近似正弦波。有关该内容的一些信息此处,QPainterPath 文档为此处

子类 QGraphicsItem() 并重写 paint() 事件以使用 QPainter 绘制路径。

Sinewave::Sinewave( QGraphicsItem* pParent ) : QGraphicsObject( pParent)
{
    m_bounds = QRectF(0,0,150,100);
}

QRectF Sinewave::boundingRect() const
{
    return m_bounds;
}

void Sinewave::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    unsigned int i;
    QPointF last(0,50);
    const int numberOfCycles = 4.0;
    painter->setRenderHint(QPainter::Antialiasing);

    for (int i = 0; i < boundingRect().width(); i++)
    {
        double radians = numberOfCycles * 2.0 * PI * static_cast<double>(i) / boundingRect().width();
        double y = sin(radians);
        QPointF next(i, 50 + y*50);
        painter->drawLine(last, next);
        last = next;
    }
}

您使用哪一个取决于您希望对象执行的操作(例如,您需要在运行时更改它的形状吗?)。

对于您有关动画的问题 - 不,您只能使用 QGraphcisItemAnimation 应用变换。我会通过使用 QTimer 来驱动您想要在其 timeout() 插槽中的项目中进行的更改来完成此操作。

For the first part of your question : Probably a couple of ways:

a. Use QPainterPath and the cubicTo() method to create the "path", then use QGraphicsPath with the path you create. In order to do this, you'll need to approximate the sinewave using bezier curves. Some info on that here, and the QPainterPath documentation is here

b. Subclass QGraphicsItem() and override the paint() event to draw your path using QPainter.

Sinewave::Sinewave( QGraphicsItem* pParent ) : QGraphicsObject( pParent)
{
    m_bounds = QRectF(0,0,150,100);
}

QRectF Sinewave::boundingRect() const
{
    return m_bounds;
}

void Sinewave::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    unsigned int i;
    QPointF last(0,50);
    const int numberOfCycles = 4.0;
    painter->setRenderHint(QPainter::Antialiasing);

    for (int i = 0; i < boundingRect().width(); i++)
    {
        double radians = numberOfCycles * 2.0 * PI * static_cast<double>(i) / boundingRect().width();
        double y = sin(radians);
        QPointF next(i, 50 + y*50);
        painter->drawLine(last, next);
        last = next;
    }
}

Which one you use would depend on what you want the object to do (e.g. do you need to change it's shape at runtime?).

For your question about animation - no, you can only apply transforms with QGraphcisItemAnimation. I would do it by using a QTimer to drive the change you want in your items in it's timeout() slot.

朮生 2024-12-11 07:11:30

尝试使用以下算法来显示正弦波:

const double PI = 3.14159267;
unsigned int i;
for (unsigned int i = 0; i < 400; ++i)
{
    double radians = static_cast<double>(i) / 100.0;
    double y = sin(radians);
    set_point(radians, y);
}

通过向“y”值添加偏移量,您可以向上或向下移动正弦波。
在“弧度”(或 X 坐标)上添加一个常数将使正弦波向左或向右移动。

Try the following algorithm for displaying a sine wave:

const double PI = 3.14159267;
unsigned int i;
for (unsigned int i = 0; i < 400; ++i)
{
    double radians = static_cast<double>(i) / 100.0;
    double y = sin(radians);
    set_point(radians, y);
}

By adding an offset to the 'y' value, you can move the sine wave up or down.
Adding a constant to the 'radians' (or X ordinate) will move the sine wave left or right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文