Issue with qSlicerWebWidget in python scripted extensions

If want to generate a pdf file using the qSlicerWebWidget I can simply type in the Python Interactor something like

w = slicer.qSlicerWebWidget()
w.webView().url = qt.QUrl("https://www.slicer.org/")
w.printToPdf(r"C:\foo\page.pdf")

but if I run the same code from a ScriptedLoadableModuleWidget it will not create the pdf. Moreover, no error message or exception is presented.

A similar behaviour applies if I want to .show() a qSlicerWebWidget. It’s working from the Python Interactor, but not from extensions.

What am I missing?

Try adding slicer.app.processEvents() after setting the url so the widget has a chance to render.

1 Like

I tried using slicer.app.processEvents(), but the same problem remains

You probably need more event processing. The best is to write the code in an asynchronous style, where you wait to process the page when it finishes loading. See the Signals section here:
https://apidocs.slicer.org/master/classqSlicerWebWidget.html

An example of use along with relevant signal connection is available at Does WebWidget's printToPdf work on Mac?

It may be helpful to move forward.