Code:
class Text(QtGui.QLabel):
def __init__(self,parent):
QtGui.QLabel.__init__(self,parent)
def mouseReleaseEvent(self,evt):
pos = evt.pos()
if pos.x() < 0 or pos.y() < 0 or pos.x() > self.width() or pos.y() > self.height():
return
self.parent().update()
self.emit(QtCore.SIGNAL('clicked()'))
def updateText(self, txt):
self.setText(txt)
class Window(QtGui.QMainWindow):
def createNews(self):
self.a1 = Text(self)
self.a1.updateText("Test")
self.a1.setFixedWidth(200)
self.a1.move(23, 173)
self.a1.show()
self.connect(self.a1, QtCore.SIGNAL("clicked()"), self.OpenURL)
def OpenURL(self, URL):
QtGui.QDesktopServices.openUrl(QtCore.QUrl(str(URL)))
I'm trying to make a "clickable" label, so that if I click it, it will open a URL passed in the argument via self.connect(self.a1, QtCore.SIGNAL("clicked()"), self.OpenURL)
.
The question is, if it's possible to pass an argument (url) to self.OpenURL()
so it could open the URL?
Firstly, you should get rid of the old-style signal syntax, and define a custom clicked
signal, like this:
class Text(QtGui.QLabel):
clicked = QtCore.pyqtSignal()
def mouseReleaseEvent(self, evt):
...
self.clicked.emit()
Then use a lambda
to send the argument with the signal:
self.a1.clicked.connect(lambda: self.OpenURL(url))
More information about new-style signal/slot syntax can be found here.