14

Clicking QLabel should open a default web browser with URL link specified. It is not happening yet. Why? A second question. Would it be possible to override the default blue color of the Label's font with something else?

enter image description here

class Widget(QtGui.QWidget):
    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)
        vLayout=QtGui.QVBoxLayout(self)
        self.setLayout(vLayout)
        urlLink="<a href=\"http://www.google.com\">'Click this link to go to Google'</a>" 
        label=QtGui.QLabel(self)
        label.setText(urlLink)
        vLayout.addWidget(label)


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())
alphanumeric
  • 17,967
  • 64
  • 244
  • 392

2 Answers2

24

The styling of the label's contents can be modified using the standard html syntax.

To automatically open external links:

    label.setOpenExternalLinks(True)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • 1
    Thanks Ekhumoro! Here it is colored black: `urlLink=" This is a link " ` – alphanumeric Sep 28 '15 at 22:54
  • 1
    It should also be noted that `setTextInteractionFlags` must have `LinksAccessibleByMouse` set. https://doc-snapshots.qt.io/qtforpython-5.15/PySide2/QtCore/Qt.html?highlight=textselectablebymouse#PySide2.QtCore.PySide2.QtCore.Qt.TextInteractionFlag – Lorem Ipsum May 26 '21 at 13:27
0

In Qt Designer,

  1. Ensure the label object containing the link is selected,
  2. Locate the openExternalLinks property within the QLabel Group in the Property Editor (you can type open into the Property Editor filter field),
  3. Set property openExternalLinks to True (checked). [This property is set to False by default.]

Qt Designer Property Editor

DaveL17
  • 1,673
  • 7
  • 24
  • 38