本文整理汇总了Python中PyQt5.QtGui.QTextDocument类的典型用法代码示例。如果您正苦于以下问题:Python QTextDocument类的具体用法?Python QTextDocument怎么用?Python QTextDocument使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QTextDocument类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: paint
def paint(self, painter, option, index):
option = QStyleOptionViewItem(option) # copy option
self.initStyleOption(option, index)
style = option.widget.style() if option.widget else QApplication.style()
doc = QTextDocument()
doc.setHtml(option.text)
# painting item without text
option.text = ""
style.drawControl(QStyle.CE_ItemViewItem, option, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
# Hilight text if item is selected
if option.state & QStyle.State_Selected:
ctx.palette.setColor(QPalette.Text, option.palette.color(QPalette.Active, QPalette.HighlightedText))
textRect = style.subElementRect(QStyle.SE_ItemViewItemText, option)
painter.save()
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
doc.documentLayout().draw(painter, ctx)
painter.restore()
示例2: paint
def paint(self, painter, option, index):
options = QStyleOptionViewItem(option)
item = index.data(Qt.UserRole)
if isinstance(item, (CommentItem, ChangeItem)):
options.decorationAlignment = Qt.AlignHCenter
self.initStyleOption(options,index)
if options.widget is None:
style = QApplication.style()
else:
style = options.widget.style()
doc = QTextDocument()
doc.setHtml(options.text)
options.text = ""
style.drawControl(QStyle.CE_ItemViewItem, options, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
# Highlighting text if item is selected
#if (optionV4.state & QStyle::State_Selected)
#ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText))
textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options)
painter.save()
painter.translate(textRect.topLeft())
painter.setClipRect(textRect.translated(-textRect.topLeft()))
doc.documentLayout().draw(painter, ctx)
painter.restore()
示例3: htmlToPlainText
def htmlToPlainText(text):
RICH_PREFIX = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" '\
'"http://www.w3.org/TR/REC-html40/strict.dtd">'
if text.startswith(RICH_PREFIX):
document = QTextDocument()
document.setHtml(text)
text = document.toPlainText()
return text
示例4: paint
def paint(self, painter, option, index):
self.initStyleOption(option, index)
foreground = index.data(Qt.ForegroundRole)
font = index.data(Qt.FontRole)
style = QApplication.style()
doc = QTextDocument()
if index.column() == HistoryTableModel.columns_types.index('pubkey'):
doc.setHtml(option.text)
else:
doc.setPlainText(option.text)
option.text = ""
style.drawControl(QStyle.CE_ItemViewItem, option, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
if foreground:
if foreground.isValid():
ctx.palette.setColor(QPalette.Text, foreground)
if font:
doc.setDefaultFont(font)
text_rect = style.subElementRect(QStyle.SE_ItemViewItemText, option)
painter.save()
painter.translate(text_rect.topLeft())
painter.setClipRect(text_rect.translated(-text_rect.topLeft()))
doc.documentLayout().draw(painter, ctx)
painter.restore()
示例5: textDocument
def textDocument(self):
td = QTextDocument()
td.setMetaInformation(QTextDocument.DocumentTitle, self.getDocumentTitle())
if self.ss:
td.setDefaultStyleSheet(self.ss)
td.setHtml(self.getHtml())
td.setDefaultFont(globalSettings.font)
return td
示例6: textDocument
def textDocument(self, title, htmltext):
td = QTextDocument()
td.setMetaInformation(QTextDocument.DocumentTitle, title)
if self.ss:
td.setDefaultStyleSheet(self.ss)
td.setHtml(htmltext)
td.setDefaultFont(globalSettings.font)
return td
示例7: sizeHint
def sizeHint(self, option, index):
self.initStyleOption(option, index)
doc = QTextDocument()
if index.column() == HistoryTableModel.columns_types.index('pubkey'):
doc.setHtml(option.text)
else:
doc.setPlainText("")
doc.setTextWidth(-1)
return QSize(doc.idealWidth(), doc.size().height())
示例8: pixmap
def pixmap(cursor, num_lines=6, scale=0.8):
"""Return a QPixmap displaying the selected lines of the document.
If the cursor has no selection, num_lines are drawn.
By default the text is drawn 0.8 * the normal font size. You can change
that by supplying the scale parameter.
"""
block = cursor.document().findBlock(cursor.selectionStart())
c2 = QTextCursor(block)
if cursor.hasSelection():
c2.setPosition(cursor.selectionEnd(), QTextCursor.KeepAnchor)
c2.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
else:
c2.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor, num_lines)
data = textformats.formatData('editor')
doc = QTextDocument()
font = QFont(data.font)
font.setPointSizeF(font.pointSizeF() * scale)
doc.setDefaultFont(font)
doc.setPlainText(c2.selection().toPlainText())
if metainfo.info(cursor.document()).highlighting:
highlighter.highlight(doc, state=tokeniter.state(block))
size = doc.size().toSize() + QSize(8, -4)
pix = QPixmap(size)
pix.fill(data.baseColors['background'])
doc.drawContents(QPainter(pix))
return pix
示例9: __init__
def __init__(self, my_book, parent=None):
super().__init__(parent)
self.book = my_book
# Where we store the last-sought-for find string
self.find_text = None
# Register to read and write metadata
my_book.get_meta_manager().register( C.MD_NO, self.read_meta, self.save_meta )
# Set our only font (we don't care about the general font, only mono)
# n.b. this gets the Book's default size as it hasn't loaded a document
# yet.
self.setFont(fonts.get_fixed(my_book.get_font_size()))
# hook up to be notified of a change in font choice
fonts.notify_me(self.font_change)
# Set up our document not using the default one
a_document = QTextDocument()
a_document.setDocumentLayout(QPlainTextDocumentLayout(a_document))
self.setDocument(a_document)
# Turn off linewrap mode
self.setLineWrapMode(QPlainTextEdit.NoWrap)
# The following kludge allows us to get the correct highlight
# color on focus-in. For unknown reasons Qt makes us use the
# "Inactive" color group even after focus-in. See focusInEvent()
# and focusOutEvent() below.
self.palette_active = QPalette(self.palette())
self.palette_inactive = QPalette(self.palette())
b = self.palette().brush(QPalette.Active,QPalette.Highlight)
self.palette_active.setBrush(QPalette.Inactive,QPalette.Highlight,b)
# Set the cursor shape to IBeam -- no idea why this supposed default
# inherited from QTextEdit, doesn't happen. But it doesn't.
self.viewport().setCursor(Qt.IBeamCursor)
# Hook up a slot to notice that the document has changed its
# modification state.
self.document().modificationChanged.connect(self.yikes)
# Create our edit menu and stow it in the menu bar. Disable it.
ed_menu = QMenu(C.ED_MENU_EDIT,self)
ed_menu.addAction(C.ED_MENU_UNDO,self.undo,QKeySequence.Undo)
ed_menu.addAction(C.ED_MENU_REDO,self.redo,QKeySequence.Redo)
ed_menu.addSeparator()
ed_menu.addAction(C.ED_MENU_CUT,self.cut,QKeySequence.Cut)
ed_menu.addAction(C.ED_MENU_COPY,self.copy,QKeySequence.Copy)
ed_menu.addAction(C.ED_MENU_PASTE,self.paste,QKeySequence.Paste)
ed_menu.addSeparator()
ed_menu.addAction(C.ED_MENU_FIND,self.find_action,QKeySequence.Find)
ed_menu.addAction(C.ED_MENU_NEXT,self.find_next_action,QKeySequence.FindNext)
self.edit_menu = mainwindow.get_menu_bar().addMenu(ed_menu)
self.edit_menu.setVisible(False)
# In order to get focus events, we need to set focus policy
self.setFocusPolicy(Qt.StrongFocus)
示例10: sizeHint
def sizeHint(self, option, index):
"""QStyledItemDelegate.sizeHint implementation
"""
options = QStyleOptionViewItem(option)
self.initStyleOption(options, index)
doc = QTextDocument()
if self._font is not None:
doc.setDefaultFont(self._font)
doc.setDocumentMargin(1)
# bad long (multiline) strings processing doc.setTextWidth(options.rect.width())
doc.setHtml(options.text)
return QSize(doc.idealWidth(), doc.size().height())
示例11: sizeHint
def sizeHint(self, option, index):
text = index.model().data(index)
doc = QTextDocument()
doc.setHtml(text)
width = index.model().data(index, Qt.UserRole + 1337)
doc.setTextWidth(width)
return QSize(doc.idealWidth(), doc.size().height())
示例12: formatText
def formatText(self, text, _type):
if not text:
return text
# if _type == "t2t":
# text = self.runT2T(text)
# elif _type == "txt":
# text = text.replace("\n", "<br>")
elif _type == "html":
doc = QTextDocument()
doc.setHtml(text)
text = doc.toPlainText()
# text = self.htmlBody(text)
return text
示例13: sizeHint
def sizeHint(self, option, index):
options = QStyleOptionViewItem(option)
self.initStyleOption(options,index)
doc = QTextDocument()
doc.setHtml(options.text)
doc.setTextWidth(options.rect.width())
return QSize(doc.idealWidth(), doc.size().height())
示例14: sizeHint
def sizeHint(self, option, index):
self.initStyleOption(option, index)
doc = QTextDocument()
doc.setHtml(option.text)
doc.setTextWidth(option.rect.width())
return QSize(doc.idealWidth(), max(doc.size().height(), option.decorationSize.height()))
示例15: test_highlighted
def test_highlighted(qtbot):
"""Make sure highlighting works.
Note that with Qt 5.11.3 and > 5.12.1 we need to call setPlainText *after*
creating the highlighter for highlighting to work. Ideally, we'd test
whether CompletionItemDelegate._get_textdoc() works properly, but testing
that is kind of hard, so we just test it in isolation here.
"""
doc = QTextDocument()
completiondelegate._Highlighter(doc, 'Hello', Qt.red)
doc.setPlainText('Hello World')
# Needed so the highlighting actually works.
edit = QTextEdit()
qtbot.addWidget(edit)
edit.setDocument(doc)
colors = [f.foreground().color() for f in doc.allFormats()]
assert QColor('red') in colors