本文整理汇总了Python中PyQt5.QtWidgets.QTextBrowser.insertHtml方法的典型用法代码示例。如果您正苦于以下问题:Python QTextBrowser.insertHtml方法的具体用法?Python QTextBrowser.insertHtml怎么用?Python QTextBrowser.insertHtml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWidgets.QTextBrowser
的用法示例。
在下文中一共展示了QTextBrowser.insertHtml方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LogWidget
# 需要导入模块: from PyQt5.QtWidgets import QTextBrowser [as 别名]
# 或者: from PyQt5.QtWidgets.QTextBrowser import insertHtml [as 别名]
class LogWidget(QWidget, MooseWidget):
"""
A widget that shows the log.
This ties into mooseutils.message.mooseMessage which will
emit a signal on mooseutils.message.messageEmitter
This widget listens to messageEmitter and puts
the text in the widget.
Color text is supported.
"""
def __init__(self, **kwds):
"""
Constructor.
"""
super(LogWidget, self).__init__(**kwds)
self.top_layout = WidgetUtils.addLayout(vertical=True)
self.setLayout(self.top_layout)
self.log = QTextBrowser(self)
self.log.setStyleSheet("QTextBrowser { background: black; color: white; }")
self.log.setReadOnly(True)
message.messageEmitter.message.connect(self._write)
self.button_layout = WidgetUtils.addLayout()
self.hide_button = WidgetUtils.addButton(self.button_layout, self, "Hide", lambda: self.hide())
self.clear_button = WidgetUtils.addButton(self.button_layout, self, "Clear", lambda: self.log.clear())
self.top_layout.addWidget(self.log)
self.top_layout.addLayout(self.button_layout)
self.resize(800, 500)
@pyqtSlot(str, str)
def _write(self, msg, color):
"""
This is the slot that will write the message to the widget.
Inputs:
msg: The message to write
color: The color to write the text in.
"""
if not msg:
return
msg = msg.encode('utf-8') # make sure if there are bad characters in the message that we can show them.
if not color or color == "None":
color = "white"
else:
color = str(color)
if msg.find("\n") >= 0:
self.log.insertHtml('<div style="color:%s"><pre><code>%s</code></pre></div><br>' % (color.lower(), escape(msg)))
else:
self.log.insertHtml('<div style="color:%s">%s</div><br>' % (color.lower(), escape(msg)))
示例2: PreviewWidgetColor
# 需要导入模块: from PyQt5.QtWidgets import QTextBrowser [as 别名]
# 或者: from PyQt5.QtWidgets.QTextBrowser import insertHtml [as 别名]
class PreviewWidgetColor(QGroupBox):
def __init__(self, parent=None):
super().__init__(parent)
self.setTitle(self.tr("Preview"))
self.setMaximumHeight(120)
vboxLayout = QVBoxLayout(self)
self.previewGroupBox = QGroupBox(self)
self.previewGroupBox.setObjectName("previewGroupBox")
vboxLayout.addWidget(self.previewGroupBox)
self.horizontalLayout = QHBoxLayout(self.previewGroupBox)
self.verticalLayout = QVBoxLayout()
self.horizontalLayout.addLayout(self.verticalLayout)
self.previewLabel = QLabel(self.previewGroupBox)
self.previewLabel.setText(self.tr("Window Text"))
self.previewLabel.setObjectName("previewLabel")
self.verticalLayout.addWidget(self.previewLabel)
self.previewPushButton = QPushButton(self.previewGroupBox)
self.previewPushButton.setText(self.tr("Button"))
self.previewPushButton.setObjectName("previewPushButton")
self.verticalLayout.addWidget(self.previewPushButton)
self.previewTextBrowser = QTextBrowser(self.previewGroupBox)
self.previewTextBrowser.setObjectName("previewTextBrowser")
self.previewTextBrowser.insertHtml(self.tr("""<style>
#unclicked {color : rgb(255,0,0);}
#sunclicked {color : rgb(255,0,255);}
#clicked {color : rgb(0,255,0);}
#sclicked {color : rgb(0,0,255);}
</style>
<p>Normal metin <a id='unclicked' href='#'>bağlantı</a> <a id='clicked' href='#'>ziyaret edilmiş</a></p>
<p>Seçili metin <a id='sunclicked' href='#'>bağlantı</a> <a id='sclicked' href='#'>ziyaret edilmiş</a></p>"""))
cursor = self.previewTextBrowser.textCursor()
cursor.setPosition(39)
cursor.setPosition(76, QTextCursor.KeepAnchor)
self.previewTextBrowser.setTextCursor(cursor)
self.horizontalLayout.addWidget(self.previewTextBrowser)
self.previewPushButton.installEventFilter(self.previewGroupBox)
self.previewPushButton.setFocusPolicy(Qt.NoFocus)
self.previewTextBrowser.installEventFilter(self.previewGroupBox)
self.previewTextBrowser.setFocusPolicy(Qt.NoFocus)
self.previewTextBrowser.setTextInteractionFlags(Qt.NoTextInteraction)
def eventFilter(self, obj, event):
if self.previewPushButton:
if event.type() == QEvent.MouseButtonRelease:
return True
elif event.type() == QEvent.MouseButtonPress:
return True
elif event.type() == QEvent.MouseButtonDblClick:
return True
else:
return False
else:
super().eventFilter(obj, event)