使用QT平滑文本动画(跑马灯文本效果)

发布于 2024-08-10 03:15:33 字数 415 浏览 10 评论 0原文

我有一个关于如何在不强大的硬件(Atom N270 + Intel 945GSM)上使选框文本平滑(使用 Qt 4.5.3)的问题。我的方法如下:

  1. 我将文本(36px Arial 粗体)绘制到 QPixmap(离屏绘画设备)上,
  2. 我设置一个计时器,每 30 毫秒向左移动 1px 内容。
  3. 当 onTimer 事件被触发时,我选择 QPixmap 并将这部分打印到窗口画布上。

一般来说,动作是流畅的。但是,我创建的字幕文本越多,CPU 使用率就越高。当文本占满屏幕 50%(1920x1680)时,CPU 使用率约为 70-80%。文本动画变得有点闪烁。如果我将定时器间隔设置为50ms或更大,就会很顺利。但文字移动速度太慢。

我的方法无效吗?我应该使用 OpenGL 吗?欢迎任何提示,并提前致谢 ^^)

I have a question about how to make marquee text smooth (using Qt 4.5.3) on not powerful hardware (Atom N270 + Intel 945GSM). My approach is as follows:

  1. I draw my text (36px Arial bold) onto a QPixmap (off-screen paint device)
  2. I set a timer to shift 1px content to left in each 30ms.
  3. When onTimer event is triggered, I make a selection of the QPixmap and print this part onto the window canvas.

Generally speaking, it moves smoothly. However, the more marquee text I create, the more CPU usage it reaches. When text can fill 50% screen (1920x1680), the CPU usage is about 70-80%. Text animation becomes a bit flickered. If I set timer interval to 50ms or larger, it will be smooth. But text moves too slow.

Is my approach ineffective? Shall I use OpenGL? Any hint is welcome and thanks in advance ^^)

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

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

发布评论

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

评论(1

草莓味的萝莉 2024-08-17 03:15:33

您应该查看图形视图框架。您可以将 QGraphicsTextItem 对象添加到 QGraphicsScene,并设置视图的坐标(缩放和平移)以使文本移动并显示您想要的任何大小,包括使某些文本“离开屏幕”(在视图之外) 。我希望这比每个计时器事件复制和绘制 QPixmap 的(不同大小)部分要高效得多。

该文档指出,图形视图框架经过优化,可以同时处理多个对象。如果结果并没有更好,您可能想尝试一下 4.6 beta。图形视图渲染即将进行优化。

顺便说一句,我会看看 QTimeLine 来帮助您驱动动画。除了经常提供基本的“刻度”之外,您还可以设置“曲线形状”,这些形状可以开始缓慢并加速,或者随着结束的临近而减速。请注意,如果您想在finished()信号后重新启动QTimeLine,如果您更改持续时间并调用start()而不调用setCurrentTime(0),则计时器将以旧的持续时间作为当前时间启动。

You should look into the Graphics View framework. You can add QGraphicsTextItem objects to a QGraphicsScene, and set the coordinates of the view (zoom and translation) to make your text move and appear whatever size you want, including having some of the text to be "off screen" (outside the view). I would expect this to be much more efficient than copying and painting a (varying size) portion of a QPixmap every timer event.

The documentation states that the Graphics View framework is optimized to handle many objects at once. If it turns out to not be any better, you might want to give the 4.6 beta a try. There are optimizations coming for the Graphics View rendering.

As an aside, I would look at QTimeLine for helping you to drive the animation. Besides providing basic "ticks" every so often, you can set "curve shapes" that can start out slow and speed up, or slow down as the end approaches. Just beware that if you want to restart your QTimeLine after the finished() signal, if you change the duration and call start() without calling setCurrentTime(0), the timer will start with the old duration as the current time.

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