QTextEdit.insertHtml() 非常慢

发布于 2024-09-07 14:10:39 字数 269 浏览 1 评论 0原文

我实际上已经放弃了让它跑得更快的尝试。

我最大的问题是,当我插入 html 时,应用程序速度慢得像爬行一样。 我有一个进度条,并且我正在调用

QCoreApplication.processEvents()

(顺便说一句,我正在使用 pyqt)

我可以将 insertHtml() 放入不同的线程中,这样我就不会出现无响应的界面吗? 我该怎么办呢?我研究过 QThread 和 QThreadPool,但不太确定从哪里开始。

I've given up on actually trying to make it go faster.

My biggest problem is that when I'm inserting the html, the application slows down to a crawl.
I have a progressbar, and I'm calling

QCoreApplication.processEvents()

(I'm using pyqt, by the way)

Can I put insertHtml() into a different thread, so I don't have an unresponsive interface?
How would I go about that? I've looked into QThread and QThreadPool, and I'm not quite sure where to begin.

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

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

发布评论

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

评论(2

此刻的回忆 2024-09-14 14:10:39

我也遇到了这个问题,以下是我为使其更快而采取的一些措施:

TxtBrows->setAcceptRichText(false);
TxtBrows->setContextMenuPolicy(Qt::NoContextMenu);
TxtBrows->setOpenLinks(false);
TxtBrows->setReadOnly(true);
TxtBrows->setUndoRedoEnabled(false);

这应该消除不必要的开销。

另外,当插入大量文本时,最好关闭屏幕更新:

setUpdatesEnabled(false);
    TxtBrows->append(SomeBigHTMLString);
setUpdatesEnabled(true);

Qt 文档中的某处建议这样做,但我现在找不到该位置。

[编辑]
我在文档中偶然发现了这个地方(正好赶上它们被 QT5 grinn 过时了)http://qt-project.org/doc/qt-4.8/qwidget.html#updatesEnabled-prop

I had this problem as well, here are a few things I did to make it faster:

TxtBrows->setAcceptRichText(false);
TxtBrows->setContextMenuPolicy(Qt::NoContextMenu);
TxtBrows->setOpenLinks(false);
TxtBrows->setReadOnly(true);
TxtBrows->setUndoRedoEnabled(false);

This should get rid of unneeded overhead.

Also when inserting large amounts of text its good to turn off screen updates:

setUpdatesEnabled(false);
    TxtBrows->append(SomeBigHTMLString);
setUpdatesEnabled(true);

This was recommended somewhere in the Qt documentation but I can't find the spot just now.

[Edit]
I stumbled across the spot in the Docs (just in time for them to be outdated by QT5 grinn) http://qt-project.org/doc/qt-4.8/qwidget.html#updatesEnabled-prop

↙温凉少女 2024-09-14 14:10:39

在 GUI 应用程序中,主线程
也称为 GUI 线程,因为
这是唯一允许的线程
执行GUI相关的操作。
-- 来自 Qt 文档

所以,不。不幸的是,您无法在线程中执行该操作。

编辑:从技术上讲,这是可能的。我只是写了一个简短的代码片段,但是以这种方式使用 Qt GUI 对象是非常不安全的。

In GUI applications, the main thread
is also called the GUI thread because
it's the only thread that is allowed
to perform GUI-related operations.
-- from the Qt Docs

So, no. Unfortunately you cannot perform that operation in a thread.

Edit: Technically, it is possible. I just wrote a short snippet that did so, however using Qt GUI objects in that way is highly unsafe.

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