QtWebkit同步加载
我使用没有 QWebView 的 QWebPage,因为我想将 HTML 文件的内容渲染到 QPixmap/QImage 上。
我希望页面加载同步完成,而不是默认异步加载。默认方式是调用 QWebFrame::setHtml() 或 QWebFrame::setContent() ,但这会异步加载图像。我想要的是某种阻塞函数调用,例如 QWebFrame::waitUntilLoadFinished(),之后我可以调用 render()
并完成它。
我找不到办法做到这一点。我错过了什么吗?
I'm using a QWebPage without a QWebView because I want to render the contents of an HTML file onto a QPixmap/QImage.
I want the loading of the page to be done synchronously, not asynchronously which is the default. The default way is to call QWebFrame::setHtml()
or QWebFrame::setContent()
, but this loads images asynchronously. What I want is some sort of blocking function call, something like QWebFrame::waitUntilLoadFinished()
after which I could just call render()
and be done with it.
I can't find a way to do this. Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果有人仍然需要它,这就是我让它发挥作用的方式。
If someone still needs it, this is how I got it working.
我建议您以 Qt 方式进行异步操作,这会很有帮助。很多。
如果您仍想在同步中执行此操作,请在单独的 QThread。
请参阅我的评论以获取详细的 QThread 使用信息。
注意:不要忘记调用 moveToThread();在线程头内,否则所有信号都将经过 QApplication 执行循环。
I suggest you to do it the Qt way and do it async, it helps a lot.
In case you still want to do it in Sync, use QEventLoop in a seperate QThread.
See my comment for detailed QThread usage information.
Note: do not forget to call moveToThread(); within the thread header, or all signals will go over the QApplication execution loop.
这种方法存在的问题是,您基本上必须等待不确定的时间,并且如果您从主线程发出调用,则会阻塞事件处理,从而阻塞诸如
loadFinished
之类的信号。虽然您可以通过显式泵送事件来解决这个问题,但我没有看到任何固有的问题可以阻止您发出页面加载,而只是在
loadFinished
时进行处理发生了。如果您需要辅助线程等待页面,您始终可以使用同步机制让这些线程等待。
That approach has the problem that you basically have to wait for an undetermined amount of time and if you're issuing the call from the main thread you're blocking event processing and thus signals like
loadFinished
.While you could get around that by explictly pumping event, i don't see an inherent problem that could prevent you from issuing the page-load and just do the processing when
loadFinished
occured.If you need secondary threads to wait for the page, you could always let those threads wait by using synchronisation mechanisms.
如果有人感兴趣,我使用特殊的“PageRasterizer”类来实现这一点。
该类在构造函数中创建一个 QWebPage 并将布尔加载标志设置为 false。
connect()
调用将loadFinished
信号连接到仅将加载标志设置为 true 的成员槽。返回图像的特殊
RenderPage()
成员函数完成所有工作:它接受 HTML 字符串并调用setHtml()
。之后是等待标志的while
循环;当标志为 false 时,将调用 qApp->processEvents(),以便发出信号并最终调用标志设置槽。如果是这样,循环就会中断,现在您可以将页面渲染到 QImage(不要忘记在返回之前将标志设置回 false)。如果您对渲染过程感兴趣,请查看 此 Qt 示例(
Thumbnailer::render()
函数)。为了获得奖励积分,您可以将此类设为函子。
If anyone's interested, I implemented this using a special "PageRasterizer" class.
The class creates a QWebPage in the constructor and sets a bool loading flag to false. A
connect()
call connects theloadFinished
signal to a member slot that merely sets the loading flag to true.A special
RenderPage()
member function that returns an image does all the work: it accepts the HTML string and callssetHtml()
. After that comes awhile
loop that waits on the flag; while the flag is false,qApp->processEvents()
is called so signals get emitted and the flag setting slot is eventually called. When it is, the loop breaks and now you can render the page to a QImage (don't forget to set the flag back to false before returning).If you're interested in the rendering process, look at this Qt example (the
Thumbnailer::render()
function).For bonus points, you can make this class a functor.