gpt4 book ai didi

c++ - QGraphicsView:蛇线和线动画

转载 作者:行者123 更新时间:2023-11-30 04:32:46 25 4
gpt4 key购买 nike

我有两个关于 QGraphicsView Framework 的问题。

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

Image: a snake line

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

提前致谢!

亲切的问候- 披 Gamma

最佳答案

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

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

继承 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() 槽中驱动你想要的项目更改来做到这一点。

关于c++ - QGraphicsView:蛇线和线动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7406810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com