SHOW:
|
|
- or go back to the newest paste.
| 1 | import sys | |
| 2 | from PySide.QtGui import * | |
| 3 | from PySide.QtCore import * | |
| 4 | ||
| 5 | ||
| 6 | class ClickableLabel(QLabel): | |
| 7 | '''Normal label, but emits an event if the label is left-clicked''' | |
| 8 | ||
| 9 | signalClicked = Signal() # emitted whenever this label is left-clicked | |
| 10 | ||
| 11 | def __init__(self, text, parent=None): | |
| 12 | super(ClickableLabel, self).__init__(text, parent) | |
| 13 | self.setStyleSheet('''
| |
| 14 | QLabel {text-decoration:none}
| |
| 15 | QLabel:hover {color:white; background:grey;}
| |
| 16 | ''') | |
| 17 | self.setFixedSize(self.sizeHint()) | |
| 18 | ||
| 19 | def mousePressEvent(self, event): | |
| 20 | if event.button() == Qt.LeftButton: | |
| 21 | self.signalClicked.emit() | |
| 22 | ||
| 23 | ||
| 24 | class HtmlLabel(QLabel): | |
| 25 | ''' | |
| 26 | Normal label, but 'text' is embedded in html, which makes the label | |
| 27 | emitting the 'linkActivated' signal on left-clicks | |
| 28 | ''' | |
| 29 | ||
| 30 | def __init__(self, text, parent=None): | |
| 31 | super(HtmlLabel, self).__init__(self.getHtml(text), parent) | |
| 32 | self.setStyleSheet('QLabel:hover {background:grey;}')
| |
| 33 | self.setFixedSize(self.sizeHint()) | |
| 34 | ||
| 35 | def getHtml(self, text): | |
| 36 | '''Return text embedded in a html link tag''' | |
| 37 | ||
| 38 | return '''<a style = "text-decoration:none" href="asdf">%s</a>''' % text | |
| 39 | ||
| 40 | ||
| 41 | class View(QWidget): | |
| 42 | '''Demonstration GUI to show use of ClickableLabel and HtmlLabel''' | |
| 43 | ||
| 44 | def __init__(self): | |
| 45 | super(View, self).__init__() | |
| 46 | self.setupUi() | |
| 47 | ||
| 48 | def setupUi(self): | |
| 49 | '''Initialize the GUI with different types of clickable labels''' | |
| 50 | ||
| 51 | - | self.resize(400, 200) |
| 51 | + | self.resize(550, 200) |
| 52 | self.layoutGeneral = QHBoxLayout(self) | |
| 53 | self.layoutGeneral.setSpacing(50) | |
| 54 | self.layoutLabel = QVBoxLayout() | |
| 55 | self.layoutGeneral.addLayout(self.layoutLabel) | |
| 56 | self.layoutDisplay = QVBoxLayout() | |
| 57 | self.layoutGeneral.addLayout(self.layoutDisplay) | |
| 58 | ||
| 59 | self.labelDate = ClickableLabel('Date')
| |
| 60 | self.labelName = ClickableLabel('Name')
| |
| 61 | self.labelAuthorHtml = HtmlLabel('Author')
| |
| 62 | ||
| 63 | self.labelDisplay = QLabel('Click on the labels on the left')
| |
| 64 | ||
| 65 | self.layoutLabel.addWidget(self.labelDate) | |
| 66 | self.layoutLabel.addWidget(self.labelName) | |
| 67 | self.layoutLabel.addWidget(self.labelAuthorHtml) | |
| 68 | ||
| 69 | self.layoutDisplay.addWidget(self.labelDisplay) | |
| 70 | ||
| 71 | ||
| 72 | ||
| 73 | class Controller(QObject): | |
| 74 | '''Controls interaction between model and view''' | |
| 75 | ||
| 76 | def __init__(self): | |
| 77 | super(Controller, self).__init__() | |
| 78 | self.view = View() | |
| 79 | self.connectSignals() | |
| 80 | ||
| 81 | def start(self): | |
| 82 | '''Start application''' | |
| 83 | ||
| 84 | self.view.show() | |
| 85 | ||
| 86 | def connectSignals(self): | |
| 87 | '''Connect the clickable labels with the corresponding slot''' | |
| 88 | ||
| 89 | self.view.labelDate.signalClicked.connect(self.onClickableLabel) | |
| 90 | self.view.labelName.signalClicked.connect(self.onClickableLabel) | |
| 91 | self.view.labelAuthorHtml.linkActivated.connect(self.onClickableLabel) | |
| 92 | ||
| 93 | def onClickableLabel(self): | |
| 94 | '''A label was clicked, show the text of the label in the display''' | |
| 95 | ||
| 96 | self.view.labelDisplay.setText('The label with the following text '\
| |
| 97 | 'was clicked:\n' + self.sender().text()) | |
| 98 | ||
| 99 | ||
| 100 | ||
| 101 | def main(): | |
| 102 | app = QApplication(sys.argv) | |
| 103 | controller = Controller() | |
| 104 | controller.start() | |
| 105 | sys.exit(app.exec_()) | |
| 106 | ||
| 107 | ||
| 108 | if __name__ == '__main__': | |
| 109 | main() |