How to display web page in a dialog using Python?

Good afternoon @pieper @jcfr @lassoan,

I am working on issue, Help Link Button #65. I would like to launch a help webpage when the user pushes the “Help” button. This seems fairly simple I know, but I finally have a little time to start learning. Is this something I can do in qreads.py? How do I associate launching my URL with the button?

Thanks!

You can open a link in the default web browser like this:

qt.QDesktopServices.openUrl(qt.QUrl('http://www.slicer.org'))

You can show a webpage embedded in the module GUI (or in a popup window within Slicer) like this:

webWidget = slicer.qSlicerWebWidget()
webWidget.url = qt.QUrl('http://www.slicer.org')
webWidget.show()

# Manually reposition the widget
slicerGeometry = slicer.util.mainWindow().geometry
webWidget.size = qt.QSize(300, 300)
webWidget.pos = qt.QPoint(slicerGeometry.x() + 20, slicerGeometry.y() + 50)

Hello @lassoan

I’m trying to use my HelpButton to launch a web page. HelpButton is defined in my qreads.ui file.

I’m getting this error after pushing the help button.
“TypeError: showSlicerQREADSHelp() takes 0 positional arguments but 1 was given”

Here is my code…

  self.ui.HelpButton.connect("clicked()", self.logic.showSlicerQREADSHelp)
  .
  .
  .

def showSlicerQREADSHelp():
    webWidget = slicer.qSlicerWebWidget()
    webWidget.url = qt.QUrl('http://www.google.com')
    webWidget.show()

    # Manually reposition the widget
    slicerGeometry = slicer.util.mainWindow().geometry
    webWidget.size = qt.QSize(300, 300)
    webWidget.pos = qt.QPoint(slicerGeometry.x() + 20, slicerGeometry.y() + 50)

You need to add self as the first argument to all non-static methods in a class.

I did that and after pushed the Help button, Slicer flashed up something, paused and then aborted.

Your local variable webWidget is getting destroyed at the end of the method. You can instead define a self.web_widget = None in the __init__ method of your class and then use it in your showSlicerQREADSHelp and then your window should be persistent.

@jamesobutler
,
Thank you so much. Took me a while to experiment until I understood scope, but I finally got it.

I’m getting there thanks to all of the help…

def setup(self):

  # Create Web Widget for HelpButton press event
  self.helpButtonWebWidget = None
  .
  .
  .
  self.ui.HelpButton.connect("clicked()", self.showSlicerQREADSHelp)
  .
  . 
  .
  def showSlicerQREADSHelp(self):
    # Create the SlicerQREADS Help webpage object
    self.helpButtonWebWidget = slicer.qSlicerWebWidget()
    self.helpButtonWebWidget.url = qt.QUrl('http://www.google.com')
    self.helpButtonWebWidget.show()

Few comments:

  • Instead of using the name helpButtonWebWidget, consider using helpWebWidget, the fact a button is used to display it is not relevant.

  • consider using properly formatted docstring for the function:

    def showSlicerQREADSHelp(self):
      """Display the help website of the application using a non-modal dialog
      """
      [...]
    
  • to ensure the existing widget is updated if the user click multiple time on the button, consider doing something like this:

    def showSlicerQREADSHelp(self):
      """Display the help website of the application using a non-modal dialog
      """
      if self.helpButtonWebWidget is None:
        self.helpButtonWebWidget = slicer.qSlicerWebWidget()
        self.helpButtonWebWidget.url = qt.QUrl('http://www.google.com')
      self.helpButtonWebWidget.show()
    
  • To reference code block in discourse, consider using the backtick. For example, here is a screenshot of how I edited your last comment:

    image

OK. Thanks JC. I want to follow the Slicer coding standards. Thanks for sharing.

  1. How do I show that ruler Ruler which is displayed at the bottom of the images? And then how do I set the sizes and colors?

  2. How you set the button icons? Are you placing them
    in resources somehow outside of QT?

Thanks.