import sys from PySide2.QtWidgets import QApplication, QLabel if __name__ == "__main__": app = QApplication(sys.argv) label = QLabel("Hello World") # Displaying html in the label label = QLabel("<font color=red size=40>Hello World</font>") label.alignment() label.show() sys.exit(app.exec_())
class labelDlg(QDialog): """ Displays a floating modal text window. If search is True, a search field editor is added on top of the window. """ def __init__(self, parent=None, title='', wSize=QSize(500, 500), scroll=True, search=False, modal=True): super().__init__(parent) self.setWindowTitle(parent.tr(title)) self.setStyleSheet( " * {background-color: rgb(220, 220, 220); color: black;}\ QLabel {selection-background-color: blue; selection-color: white}\ QLineEdit {background-color: white;}") self.setModal(modal) self.label = QLabel() self.label.setAlignment(Qt.AlignTop) #self.label.setStyleSheet("selection-background-color: blue; selection-color: white}") vl = QVBoxLayout() if search: ed = QLineEdit() ed.setMaximumWidth(300) ed.setPlaceholderText('Search') vl = QVBoxLayout() hl = QHBoxLayout() hl.addWidget(ed) button = QPushButton('Next') button.setAutoDefault(False) button.setMaximumWidth(60) hl.addWidget(button) vl.addLayout(hl) matches = [] current = 0 def f(searchedText): import re nonlocal current matches.clear() current = 0 matches.extend([ m.span() for m in re.finditer( searchedText, self.label.text(), re.IGNORECASE | re.MULTILINE | re.DOTALL) ]) if matches: item = matches[0] self.label.setSelection(item[0], item[1] - item[0]) metrics = self.label.fontMetrics() tabSize = 4 rect = metrics.boundingRect( 0, 0, 150000, 150000, self.label.alignment() | Qt.TextExpandTabs, self.label.text()[:item[1]], tabSize) scarea.ensureVisible(0, rect.height()) def g(): nonlocal current if not matches or not button.isDown(): return current = (current + 1) % len(matches) item = matches[current] self.label.setSelection(item[0], item[1] - item[0]) metrics = self.label.fontMetrics() tabSize = 4 rect = metrics.boundingRect( 0, 0, 150000, 150000, self.label.alignment() | Qt.TextExpandTabs, self.label.text()[:item[1]], tabSize) scarea.ensureVisible(0, rect.height()) button.pressed.connect(g) ed.textEdited.connect(f) if scroll: scarea = QScrollArea() scarea.setWidget(self.label) scarea.setWidgetResizable(True) vl.addWidget(scarea) else: vl.addWidget(self.label) self.setLayout(vl) self.setFixedSize(wSize) def wrapped(self, s): """ Returns wrapped text, according to the current font and size of label. NOT updated when these parameters are modified. @param s: text to wrap @type s: str @return: @rtype: list of str """ metrics = self.label.fontMetrics() tabSize = 4 # get max character count per line testText = 'WWWWWWWWWWWWWWW' # start from a minimum width ! while metrics.boundingRect(0, 0, 150000, 150000, self.label.alignment() | Qt.TextExpandTabs, testText, tabSize).width() < self.label.width(): testText += 'W' # wrap text while keeping existing newlines s = '\n'.join([ '\n'.join( textwrap.wrap(line, len(testText), break_long_words=False, replace_whitespace=False)) for line in s.splitlines() ]) return s