本文整理汇总了Python中PyQt5.QtGui.QTextDocument.setDefaultFont方法的典型用法代码示例。如果您正苦于以下问题:Python QTextDocument.setDefaultFont方法的具体用法?Python QTextDocument.setDefaultFont怎么用?Python QTextDocument.setDefaultFont使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtGui.QTextDocument
的用法示例。
在下文中一共展示了QTextDocument.setDefaultFont方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: paint
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
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()
示例2: paint
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
def paint(self, painter, option, index):
text = index.model().data(index, Qt.DisplayRole)
document = QTextDocument()
# metrics = QFontMetrics(document.defaultFont())
metrics = painter.fontMetrics()
# font = QFont()
# font.setPointSize(12)
document.setDefaultFont(G.entriesFont)
if index.column() == 0:
document.setTextWidth(69)
elif index.column() == 1:
document.setTextWidth(514)
if option.state & QStyle.State_Selected:
document.setHtml("<b bgcolor=#E6E600> <font size={} font color={}>{}"
"</font></b>".format("2", G.selectionColor, text))
else:
w = metrics.boundingRect('W').width()
# print(w)
txt = text[0:(514*4)//w]
document.setHtml("<p align (center) bgcolor=white> <font size={} "
"font color={}>{}"
"</font></p>".format("2", "black", txt))
painter.save()
painter.translate(option.rect.x(), option.rect.y())
document.drawContents(painter)
painter.restore()
示例3: pixmap
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
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
示例4: textDocument
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
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
示例5: textDocument
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
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: sizeHint
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
def sizeHint(self, option, index):
fm = option.fontMetrics
if index.column() == residNum:
return QSize(fm.width("9,999,999"), fm.height())
if index.column() == residName:
text = index.model().data(index)
document = QTextDocument()
document.setDefaultFont(option.font)
document.setHtml(text)
return QSize(document.idealWidth() + 5, fm.height())
return QStyledItemDelegate.sizeHint(self, option, index)
示例7: sizeHint
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
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())
示例8: html_copy
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
def html_copy(cursor, scheme='editor', number_lines=False):
"""Return a new QTextDocument with highlighting set as HTML textcharformats.
The cursor is a cursor of a document.Document instance. If the cursor
has a selection, only the selection is put in the new document.
If number_lines is True, line numbers are added.
"""
data = textformats.formatData(scheme)
doc = QTextDocument()
doc.setDefaultFont(data.font)
doc.setPlainText(cursor.document().toPlainText())
if metainfo.info(cursor.document()).highlighting:
highlight(doc, mapping(data), ly.lex.state(documentinfo.mode(cursor.document())))
if cursor.hasSelection():
# cut out not selected text
start, end = cursor.selectionStart(), cursor.selectionEnd()
cur1 = QTextCursor(doc)
cur1.setPosition(start, QTextCursor.KeepAnchor)
cur2 = QTextCursor(doc)
cur2.setPosition(end)
cur2.movePosition(QTextCursor.End, QTextCursor.KeepAnchor)
cur2.removeSelectedText()
cur1.removeSelectedText()
if number_lines:
c = QTextCursor(doc)
f = QTextCharFormat()
f.setBackground(QColor('#eeeeee'))
if cursor.hasSelection():
num = cursor.document().findBlock(cursor.selectionStart()).blockNumber() + 1
last = cursor.document().findBlock(cursor.selectionEnd())
else:
num = 1
last = cursor.document().lastBlock()
lastnum = last.blockNumber() + 1
padding = len(format(lastnum))
block = doc.firstBlock()
while block.isValid():
c.setPosition(block.position())
c.setCharFormat(f)
c.insertText('{0:>{1}d} '.format(num, padding))
block = block.next()
num += 1
return doc
示例9: paint
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
def paint(self, painter, option, index):
"""QStyledItemDelegate.paint implementation
"""
option.state &= ~QStyle.State_HasFocus # never draw focus rect
options = QStyleOptionViewItem(option)
self.initStyleOption(options,index)
style = QApplication.style() if options.widget is None else options.widget.style()
doc = QTextDocument()
doc.setDocumentMargin(1)
doc.setHtml(options.text)
if options.widget is not None:
doc.setDefaultFont(options.widget.font())
# bad long (multiline) strings processing doc.setTextWidth(options.rect.width())
options.text = ""
style.drawControl(QStyle.CE_ItemViewItem, options, painter);
ctx = QAbstractTextDocumentLayout.PaintContext()
# Highlighting 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, options)
painter.save()
painter.translate(textRect.topLeft())
"""Original example contained line
painter.setClipRect(textRect.translated(-textRect.topLeft()))
but text is drawn clipped with it on kubuntu 12.04
"""
doc.documentLayout().draw(painter, ctx)
painter.restore()
示例10: paint
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
def paint(self, painter, option, index):
"""QStyledItemDelegate.paint implementation
"""
option.state &= ~QStyle.State_HasFocus # never draw focus rect
option.state |= QStyle.State_Active # draw fuzzy-open completion as focused, even if focus is on the line edit
options = QStyleOptionViewItem(option)
self.initStyleOption(options, index)
style = QApplication.style() if options.widget is None else options.widget.style()
doc = QTextDocument()
if self._font is not None:
doc.setDefaultFont(self._font)
doc.setDocumentMargin(1)
doc.setHtml(options.text)
# bad long (multiline) strings processing doc.setTextWidth(options.rect.width())
options.text = ""
style.drawControl(QStyle.CE_ItemViewItem, options, painter)
ctx = QAbstractTextDocumentLayout.PaintContext()
# Highlighting text if item is selected
if option.state & QStyle.State_Selected:
ctx.palette.setColor(QPalette.Text, option.palette.color(QPalette.Active, QPalette.Text))
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()
示例11: CompletionItemDelegate
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
#.........这里部分代码省略.........
# if multiple colors are set, use different colors per column
color = colors[col % len(colors)]
self._painter.setPen(color)
ctx = QAbstractTextDocumentLayout.PaintContext()
ctx.palette.setColor(QPalette.Text, self._painter.pen().color())
if clip.isValid():
self._painter.setClipRect(clip)
ctx.clip = clip
self._doc.documentLayout().draw(self._painter, ctx)
self._painter.restore()
def _get_textdoc(self, index):
"""Create the QTextDocument of an item.
Args:
index: The QModelIndex of the item to draw.
"""
# FIXME we probably should do eliding here. See
# qcommonstyle.cpp:viewItemDrawText
# https://github.com/qutebrowser/qutebrowser/issues/118
text_option = QTextOption()
if self._opt.features & QStyleOptionViewItem.WrapText:
text_option.setWrapMode(QTextOption.WordWrap)
else:
text_option.setWrapMode(QTextOption.ManualWrap)
text_option.setTextDirection(self._opt.direction)
text_option.setAlignment(QStyle.visualAlignment(
self._opt.direction, self._opt.displayAlignment))
if self._doc is not None:
self._doc.deleteLater()
self._doc = QTextDocument(self)
self._doc.setDefaultFont(self._opt.font)
self._doc.setDefaultTextOption(text_option)
self._doc.setDocumentMargin(2)
assert _cached_stylesheet is not None
self._doc.setDefaultStyleSheet(_cached_stylesheet)
if index.parent().isValid():
view = self.parent()
pattern = view.pattern
columns_to_filter = index.model().columns_to_filter(index)
if index.column() in columns_to_filter and pattern:
repl = r'<span class="highlight">\g<0></span>'
pat = html.escape(re.escape(pattern)).replace(r'\ ', r'|')
txt = html.escape(self._opt.text)
text = re.sub(pat, repl, txt, flags=re.IGNORECASE)
self._doc.setHtml(text)
else:
self._doc.setPlainText(self._opt.text)
else:
self._doc.setHtml(
'<span style="font: {};">{}</span>'.format(
html.escape(config.val.fonts.completion.category),
html.escape(self._opt.text)))
def _draw_focus_rect(self):
"""Draw the focus rectangle of an ItemViewItem."""
state = self._opt.state
if not state & QStyle.State_HasFocus:
return
o = self._opt
o.rect = self._style.subElementRect(
self._style.SE_ItemViewItemFocusRect, self._opt, self._opt.widget)
示例12: paint
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
def paint(self, painter, option, index):
assert isinstance(painter, QPainter)
if index.data(Qt.UserRole+1):
if app_constants.HIGH_QUALITY_THUMBS:
painter.setRenderHint(QPainter.SmoothPixmapTransform)
painter.setRenderHint(QPainter.Antialiasing)
gallery = index.data(Qt.UserRole+1)
title = gallery.title
artist = gallery.artist
title_color = app_constants.GRID_VIEW_TITLE_COLOR
artist_color = app_constants.GRID_VIEW_ARTIST_COLOR
label_color = app_constants.GRID_VIEW_LABEL_COLOR
# Enable this to see the defining box
#painter.drawRect(option.rect)
# define font size
if 20 > len(title) > 15:
title_size = "font-size:{}px;".format(self.font_size)
elif 30 > len(title) > 20:
title_size = "font-size:{}px;".format(self.font_size-1)
elif 40 > len(title) >= 30:
title_size = "font-size:{}px;".format(self.font_size-2)
elif 50 > len(title) >= 40:
title_size = "font-size:{}px;".format(self.font_size-3)
elif len(title) >= 50:
title_size = "font-size:{}px;".format(self.font_size-4)
else:
title_size = "font-size:{}px;".format(self.font_size)
if 30 > len(artist) > 20:
artist_size = "font-size:{}px;".format(self.font_size)
elif 40 > len(artist) >= 30:
artist_size = "font-size:{}px;".format(self.font_size-1)
elif len(artist) >= 40:
artist_size = "font-size:{}px;".format(self.font_size-2)
else:
artist_size = "font-size:{}px;".format(self.font_size)
#painter.setPen(QPen(Qt.NoPen))
#option.rect = option.rect.adjusted(11, 10, 0, 0)
option.rect.setWidth(self.W)
option.rect.setHeight(self.H)
rec = option.rect.getRect()
x = rec[0]
y = rec[1]
w = rec[2]
h = rec[3]
text_area = QTextDocument()
text_area.setDefaultFont(option.font)
text_area.setHtml("""
<head>
<style>
#area
{{
display:flex;
width:{6}px;
height:{7}px
}}
#title {{
position:absolute;
color: {4};
font-weight:bold;
{0}
}}
#artist {{
position:absolute;
color: {5};
top:20px;
right:0;
{1}
}}
</style>
</head>
<body>
<div id="area">
<center>
<div id="title">{2}
</div>
<div id="artist">{3}
</div>
</div>
</center>
</body>
""".format(title_size, artist_size, title, artist, title_color, artist_color,
130+app_constants.SIZE_FACTOR, 1+app_constants.SIZE_FACTOR))
text_area.setTextWidth(w)
#chapter_area = QTextDocument()
#chapter_area.setDefaultFont(option.font)
#chapter_area.setHtml("""
#<font color="black">{}</font>
#""".format("chapter"))
#chapter_area.setTextWidth(w)
def center_img(width):
new_x = x
if width < w:
diff = w - width
offset = diff//2
new_x += offset
#.........这里部分代码省略.........
示例13: paint
# 需要导入模块: from PyQt5.QtGui import QTextDocument [as 别名]
# 或者: from PyQt5.QtGui.QTextDocument import setDefaultFont [as 别名]
def paint(self, painter, option, index):
assert isinstance(painter, QPainter)
if index.data(Qt.UserRole+1):
if app_constants.HIGH_QUALITY_THUMBS:
painter.setRenderHint(QPainter.SmoothPixmapTransform)
painter.setRenderHint(QPainter.Antialiasing)
gallery = index.data(Qt.UserRole+1)
title = gallery.title
artist = gallery.artist
title_color = app_constants.GRID_VIEW_TITLE_COLOR
artist_color = app_constants.GRID_VIEW_ARTIST_COLOR
label_color = app_constants.GRID_VIEW_LABEL_COLOR
# Enable this to see the defining box
#painter.drawRect(option.rect)
# define font size
if 20 > len(title) > 15:
title_size = "font-size:{}px;".format(self.font_size)
elif 30 > len(title) > 20:
title_size = "font-size:{}px;".format(self.font_size-1)
elif 40 > len(title) >= 30:
title_size = "font-size:{}px;".format(self.font_size-2)
elif 50 > len(title) >= 40:
title_size = "font-size:{}px;".format(self.font_size-3)
elif len(title) >= 50:
title_size = "font-size:{}px;".format(self.font_size-4)
else:
title_size = "font-size:{}px;".format(self.font_size)
if 30 > len(artist) > 20:
artist_size = "font-size:{}px;".format(self.font_size)
elif 40 > len(artist) >= 30:
artist_size = "font-size:{}px;".format(self.font_size-1)
elif len(artist) >= 40:
artist_size = "font-size:{}px;".format(self.font_size-2)
else:
artist_size = "font-size:{}px;".format(self.font_size)
#painter.setPen(QPen(Qt.NoPen))
#option.rect = option.rect.adjusted(11, 10, 0, 0)
option.rect.setWidth(self.W)
option.rect.setHeight(self.H)
rec = option.rect.getRect()
x = rec[0]
y = rec[1]
w = rec[2]
h = rec[3]
text_area = QTextDocument()
text_area.setDefaultFont(option.font)
text_area.setHtml("""
<head>
<style>
#area
{{
display:flex;
width:140px;
height:10px
}}
#title {{
position:absolute;
color: {4};
font-weight:bold;
{0}
}}
#artist {{
position:absolute;
color: {5};
top:20px;
right:0;
{1}
}}
</style>
</head>
<body>
<div id="area">
<center>
<div id="title">{2}
</div>
<div id="artist">{3}
</div>
</div>
</center>
</body>
""".format(title_size, artist_size, title, artist, title_color, artist_color))
text_area.setTextWidth(w)
#chapter_area = QTextDocument()
#chapter_area.setDefaultFont(option.font)
#chapter_area.setHtml("""
#<font color="black">{}</font>
#""".format("chapter"))
#chapter_area.setTextWidth(w)
def center_img(width):
new_x = x
if width < w:
diff = w - width
offset = diff//2
new_x += offset
return new_x
#.........这里部分代码省略.........