gpt4 book ai didi

c++ - 如何在 QML 中显示来自 QTextDocument 的分页文本?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:36 24 4
gpt4 key购买 nike

我目前有一个继承自 QQuickPaintedItem 的 C++ 类。我使用它通过 QTextDocument::drawContents(或直接调用其 QTextDocumenLayout 的 draw 方法)从 QTextDocument 绘制布局、分页的富文本。

但是,如 QQuickPaintedItems 文档中所述,需要注意线程问题:

Warning: Extreme caution must be used when creating QObjects, emitting signals, starting timers and similar inside this function as these will have affinity to the rendering thread.

具体来说,在这种情况下,QTextDocumentLayoutPrivate 具有计时器,当调用 QTextDocumentLayout::draw 时,计时器会启动/停止。不幸的是,QTextDocument 和计时器位于 qml 主线程中,而 paint 在渲染线程中被调用,导致消息如

QBasicTimer::start: Timers cannot be started from another thread

虽然这不会影响我的应用程序的功能(到目前为止),但这可能不是一件好事™。

因此,我的问题是是否有更好的方法在 QML 中显示分页文本(不一定涉及 QQuickPaintedItem)。

最佳答案

目前我仍在使用 QQuickPaintedItem,当调用 paint 时,我执行以下操作:

  • 首先检查QTextDocument 是否将其关联设置为其当前线程。如果是,我将照常进行。

  • 否则 QMetaObject::invokeMethod 用于调用将文档移动到渲染线程的方法,并调用 update 触发重绘,现在工作线程亲和性是正确的。在 paint 结束时,QTextDocument 的线程关联设置回原始线程。

据我所知这是有效的(例如,不再有警告),但在概念上感觉相当错误。

关于c++ - 如何在 QML 中显示来自 QTextDocument 的分页文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49221611/

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