当前位置: 首页>>代码示例>>Python>>正文


Python QPainter.drawText方法代码示例

本文整理汇总了Python中PyQt5.Qt.QPainter.drawText方法的典型用法代码示例。如果您正苦于以下问题:Python QPainter.drawText方法的具体用法?Python QPainter.drawText怎么用?Python QPainter.drawText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PyQt5.Qt.QPainter的用法示例。


在下文中一共展示了QPainter.drawText方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: paintEvent

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
 def paintEvent(self, event):
     canvas_size = self.rect()
     width = self.current_pixmap_size.width()
     extrax = canvas_size.width() - width
     if extrax < 0:
         extrax = 0
     x = int(extrax/2.)
     height = self.current_pixmap_size.height()
     extray = canvas_size.height() - height
     if extray < 0:
         extray = 0
     y = int(extray/2.)
     target = QRect(x, y, width, height)
     p = QPainter(self)
     p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
     try:
         dpr = self.devicePixelRatioF()
     except AttributeError:
         dpr = self.devicePixelRatio()
     spmap = self.pixmap.scaled(target.size() * dpr, Qt.KeepAspectRatio, Qt.SmoothTransformation)
     spmap.setDevicePixelRatio(dpr)
     p.drawPixmap(target, spmap)
     if gprefs['bd_overlay_cover_size']:
         sztgt = target.adjusted(0, 0, 0, -4)
         f = p.font()
         f.setBold(True)
         p.setFont(f)
         sz = u'\u00a0%d x %d\u00a0'%(self.pixmap.width(), self.pixmap.height())
         flags = Qt.AlignBottom|Qt.AlignRight|Qt.TextSingleLine
         szrect = p.boundingRect(sztgt, flags, sz)
         p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200))
         p.setPen(QPen(QColor(255,255,255)))
         p.drawText(sztgt, flags, sz)
     p.end()
开发者ID:bwhitenb5e,项目名称:calibre,代码行数:36,代码来源:book_details.py

示例2: paint_line_numbers

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
    def paint_line_numbers(self, ev):
        painter = QPainter(self.line_number_area)
        painter.fillRect(ev.rect(), self.line_number_palette.color(QPalette.Base))

        block = self.firstVisibleBlock()
        num = block.blockNumber()
        top = int(self.blockBoundingGeometry(block).translated(self.contentOffset()).top())
        bottom = top + int(self.blockBoundingRect(block).height())
        current = self.textCursor().block().blockNumber()
        painter.setPen(self.line_number_palette.color(QPalette.Text))

        while block.isValid() and top <= ev.rect().bottom():
            if block.isVisible() and bottom >= ev.rect().top():
                if current == num:
                    painter.save()
                    painter.setPen(self.line_number_palette.color(QPalette.BrightText))
                    f = QFont(self.font())
                    f.setBold(True)
                    painter.setFont(f)
                    self.last_current_lnum = (top, bottom - top)
                painter.drawText(
                    0, top, self.line_number_area.width() - 5, self.fontMetrics().height(), Qt.AlignRight, str(num + 1)
                )
                if current == num:
                    painter.restore()
            block = block.next()
            top = bottom
            bottom = top + int(self.blockBoundingRect(block).height())
            num += 1
开发者ID:timpalpant,项目名称:calibre,代码行数:31,代码来源:text.py

示例3: paintEvent

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
 def paintEvent(self, ev):
     shown = self.button.isChecked()
     ls = self.fm.lineSpacing()
     painter = QPainter(self)
     if self.mouse_over:
         tool = QStyleOption()
         tool.rect = self.rect()
         tool.state = QStyle.State_Raised | QStyle.State_Active | QStyle.State_MouseOver
         s = self.style()
         s.drawPrimitive(QStyle.PE_PanelButtonTool, tool, painter, self)
     painter.drawText(
         0, 0,
         self.width(),
         ls, Qt.AlignCenter | Qt.TextSingleLine, self.text)
     text = _('Hide') if shown else _('Show')
     f = self.font()
     f.setBold(True)
     painter.setFont(f)
     painter.drawText(
         0, self.height() - ls,
         self.width(),
         ls, Qt.AlignCenter | Qt.TextSingleLine, text)
     x = (self.width() - ICON_SZ) // 2
     y = ls + (self.height() - ICON_SZ - 2 * ls) // 2
     pmap = self.bright_icon if shown else self.dull_icon
     painter.drawPixmap(x, y, pmap)
     painter.end()
开发者ID:JimmXinu,项目名称:calibre,代码行数:29,代码来源:layout_menu.py

示例4: paintEvent

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
 def paintEvent(self, ev):
     painter = QPainter(self)
     painter.setRenderHint(QPainter.TextAntialiasing)
     f = painter.font()
     f.setBold(True)
     f.setPixelSize(self.height() - 1)
     painter.setFont(f)
     painter.setPen(QColor('red' if self.is_enabled else 'green'))
     painter.drawText(self.rect(), Qt.AlignCenter, 'Z')
     painter.end()
开发者ID:kovidgoyal,项目名称:vise,代码行数:12,代码来源:status_bar.py

示例5: paintEvent

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
 def paintEvent(self, ev):
     p = QPainter(self)
     p.setClipRect(ev.rect())
     bg = self.palette().color(QPalette.AlternateBase)
     if self.hovering:
         bg = bg.lighter(115)
     p.fillRect(self.rect(), bg)
     try:
         p.drawText(self.rect(), Qt.AlignLeft|Qt.AlignVCenter|Qt.TextSingleLine, self.rendered_text)
     finally:
         p.end()
开发者ID:JimmXinu,项目名称:calibre,代码行数:13,代码来源:live_css.py

示例6: message_image

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
def message_image(text, width=500, height=400, font_size=20):
    init_environment()
    img = QImage(width, height, QImage.Format_ARGB32)
    img.fill(Qt.white)
    p = QPainter(img)
    f = QFont()
    f.setPixelSize(font_size)
    p.setFont(f)
    r = img.rect().adjusted(10, 10, -10, -10)
    p.drawText(r, Qt.AlignJustify | Qt.AlignVCenter | Qt.TextWordWrap, text)
    p.end()
    return pixmap_to_data(img)
开发者ID:artbycrunk,项目名称:calibre,代码行数:14,代码来源:covers.py

示例7: paintEvent

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
 def paintEvent(self, ev):
     br = ev.region().boundingRect()
     QWidget.paintEvent(self, ev)
     p = QPainter(self)
     p.setClipRect(br)
     f = p.font()
     f.setBold(True)
     f.setPointSize(20)
     p.setFont(f)
     p.setPen(Qt.SolidLine)
     r = QRect(0, self.dummy.geometry().top() + 10, self.geometry().width(), 150)
     p.drawText(r, Qt.AlignHCenter | Qt.AlignTop | Qt.TextSingleLine, self.text)
     p.end()
开发者ID:nospy,项目名称:calibre,代码行数:15,代码来源:main.py

示例8: create_icon

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
def create_icon(text, palette=None, sz=32, divider=2):
    if palette is None:
        palette = QApplication.palette()
    img = QImage(sz, sz, QImage.Format_ARGB32)
    img.fill(Qt.transparent)
    p = QPainter(img)
    p.setRenderHints(p.TextAntialiasing | p.Antialiasing)
    qDrawShadeRect(p, img.rect(), palette, fill=QColor('#ffffff'), lineWidth=1, midLineWidth=1)
    f = p.font()
    f.setFamily('Liberation Sans'), f.setPixelSize(sz // divider), f.setBold(True)
    p.setFont(f), p.setPen(Qt.black)
    p.drawText(img.rect().adjusted(2, 2, -2, -2), Qt.AlignCenter, text)
    p.end()
    return QIcon(QPixmap.fromImage(img))
开发者ID:Ralnoc,项目名称:calibre,代码行数:16,代码来源:widget.py

示例9: create_icon

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
def create_icon(text, palette=None, sz=None, divider=2, fill='white'):
    if isinstance(fill, basestring):
        fill = QColor(fill)
    sz = sz or int(math.ceil(tprefs['toolbar_icon_size'] * QApplication.instance().devicePixelRatio()))
    if palette is None:
        palette = QApplication.palette()
    img = QImage(sz, sz, QImage.Format_ARGB32)
    img.fill(Qt.transparent)
    p = QPainter(img)
    p.setRenderHints(p.TextAntialiasing | p.Antialiasing)
    if fill is not None:
        qDrawShadeRect(p, img.rect(), palette, fill=fill, lineWidth=1, midLineWidth=1)
    f = p.font()
    f.setFamily('Liberation Sans'), f.setPixelSize(int(sz // divider)), f.setBold(True)
    p.setFont(f), p.setPen(Qt.black)
    p.drawText(img.rect().adjusted(2, 2, -2, -2), Qt.AlignCenter, text)
    p.end()
    return QIcon(QPixmap.fromImage(img))
开发者ID:davidfor,项目名称:calibre,代码行数:20,代码来源:widget.py

示例10: generate_masthead

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
def generate_masthead(title, output_path=None, width=600, height=60, as_qimage=False, font_family=None):
    init_environment()
    font_family = font_family or cprefs['title_font_family'] or 'Liberation Serif'
    img = QImage(width, height, QImage.Format_ARGB32)
    img.fill(Qt.white)
    p = QPainter(img)
    f = QFont(font_family)
    f.setPixelSize((height * 3) // 4), f.setBold(True)
    p.setFont(f)
    p.drawText(img.rect(), Qt.AlignLeft | Qt.AlignVCenter, sanitize(title))
    p.end()
    if as_qimage:
        return img
    data = pixmap_to_data(img)
    if output_path is None:
        return data
    with open(output_path, 'wb') as f:
        f.write(data)
开发者ID:Aliminator666,项目名称:calibre,代码行数:20,代码来源:covers.py

示例11: failed_img

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
 def failed_img(self):
     if self._failed_img is None:
         i = QImage(200, 150, QImage.Format_ARGB32)
         i.fill(Qt.white)
         p = QPainter(i)
         r = i.rect().adjusted(10, 10, -10, -10)
         n = QPen(Qt.DashLine)
         n.setColor(Qt.black)
         p.setPen(n)
         p.drawRect(r)
         p.setPen(Qt.black)
         f = self.font()
         f.setPixelSize(20)
         p.setFont(f)
         p.drawText(r.adjusted(10, 0, -10, 0), Qt.AlignCenter | Qt.TextWordWrap, _('Image could not be rendered'))
         p.end()
         self._failed_img = QPixmap.fromImage(i)
     return self._failed_img
开发者ID:AtulKumar2,项目名称:calibre,代码行数:20,代码来源:view.py

示例12: paintEvent

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
    def paintEvent(self, event):
        w = self.viewport().rect().width()
        painter = QPainter(self.viewport())
        painter.setClipRect(event.rect())
        floor = event.rect().bottom()
        ceiling = event.rect().top()
        fv = self.firstVisibleBlock().blockNumber()
        origin = self.contentOffset()
        doc = self.document()
        lines = []

        for num, text in self.headers:
            top, bot = num, num + 3
            if bot < fv:
                continue
            y_top = self.blockBoundingGeometry(doc.findBlockByNumber(top)).translated(origin).y()
            y_bot = self.blockBoundingGeometry(doc.findBlockByNumber(bot)).translated(origin).y()
            if max(y_top, y_bot) < ceiling:
                continue
            if min(y_top, y_bot) > floor:
                break
            painter.setFont(self.heading_font)
            br = painter.drawText(3, y_top, w, y_bot - y_top - 5, Qt.TextSingleLine, text)
            painter.setPen(QPen(self.palette().text(), 2))
            painter.drawLine(0, br.bottom()+3, w, br.bottom()+3)

        for top, bot, kind in self.changes:
            if bot < fv:
                continue
            y_top = self.blockBoundingGeometry(doc.findBlockByNumber(top)).translated(origin).y()
            y_bot = self.blockBoundingGeometry(doc.findBlockByNumber(bot)).translated(origin).y()
            if max(y_top, y_bot) < ceiling:
                continue
            if min(y_top, y_bot) > floor:
                break
            if y_top != y_bot:
                painter.fillRect(0,  y_top, w, y_bot - y_top, self.diff_backgrounds[kind])
            lines.append((y_top, y_bot, kind))
            if top in self.images:
                img, maxw = self.images[top][:2]
                if bot > top + 1 and not img.isNull():
                    y_top = self.blockBoundingGeometry(doc.findBlockByNumber(top+1)).translated(origin).y() + 3
                    y_bot -= 3
                    scaled, imgw, imgh = fit_image(img.width(), img.height(), w - 3, y_bot - y_top)
                    painter.setRenderHint(QPainter.SmoothPixmapTransform, True)
                    painter.drawPixmap(QRect(3, y_top, imgw, imgh), img)

        painter.end()
        PlainTextEdit.paintEvent(self, event)
        painter = QPainter(self.viewport())
        painter.setClipRect(event.rect())
        for top, bottom, kind in sorted(lines, key=lambda (t, b, k):{'replace':0}.get(k, 1)):
            painter.setPen(QPen(self.diff_foregrounds[kind], 1))
            painter.drawLine(0, top, w, top)
            painter.drawLine(0, bottom - 1, w, bottom - 1)
开发者ID:AtulKumar2,项目名称:calibre,代码行数:57,代码来源:view.py

示例13: paintEvent

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
    def paintEvent(self, event):
        pmap = self.blank if self.pixmap is None or self.pixmap.isNull() else self.pixmap
        target = self.rect()
        scaled, width, height = fit_image(pmap.width(), pmap.height(), target.width(), target.height())
        target.setRect(target.x(), target.y(), width, height)
        p = QPainter(self)
        p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
        p.drawPixmap(target, pmap)

        if self.pixmap is not None and not self.pixmap.isNull():
            sztgt = target.adjusted(0, 0, 0, -4)
            f = p.font()
            f.setBold(True)
            p.setFont(f)
            sz = u'\u00a0%d x %d\u00a0'%(self.pixmap.width(), self.pixmap.height())
            flags = Qt.AlignBottom|Qt.AlignRight|Qt.TextSingleLine
            szrect = p.boundingRect(sztgt, flags, sz)
            p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200))
            p.setPen(QPen(QColor(255,255,255)))
            p.drawText(sztgt, flags, sz)
        p.end()
开发者ID:davidfor,项目名称:calibre,代码行数:23,代码来源:diff.py

示例14: paint_line_numbers

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
    def paint_line_numbers(self, ev):
        painter = QPainter(self.line_number_area)
        painter.fillRect(ev.rect(), self.line_number_palette.color(QPalette.Base))

        block = self.firstVisibleBlock()
        num = block.blockNumber()
        top = int(self.blockBoundingGeometry(block).translated(self.contentOffset()).top())
        bottom = top + int(self.blockBoundingRect(block).height())
        painter.setPen(self.line_number_palette.color(QPalette.Text))
        change_starts = {x[0] for x in self.changes}

        while block.isValid() and top <= ev.rect().bottom():
            r = ev.rect()
            if block.isVisible() and bottom >= r.top():
                text = unicode(self.line_number_map.get(num, ''))
                is_start = text != '-' and num in change_starts
                if is_start:
                    painter.save()
                    f = QFont(self.font())
                    f.setBold(True)
                    painter.setFont(f)
                    painter.setPen(self.line_number_palette.color(QPalette.BrightText))
                if text == '-':
                    painter.drawLine(r.left() + 2, (top + bottom)//2, r.right() - 2, (top + bottom)//2)
                else:
                    if self.right:
                        painter.drawText(r.left() + 3, top, r.right(), self.fontMetrics().height(),
                                Qt.AlignLeft, text)
                    else:
                        painter.drawText(r.left() + 2, top, r.right() - 5, self.fontMetrics().height(),
                                Qt.AlignRight, text)
                if is_start:
                    painter.restore()
            block = block.next()
            top = bottom
            bottom = top + int(self.blockBoundingRect(block).height())
            num += 1
开发者ID:AtulKumar2,项目名称:calibre,代码行数:39,代码来源:view.py

示例15: paintEvent

# 需要导入模块: from PyQt5.Qt import QPainter [as 别名]
# 或者: from PyQt5.Qt.QPainter import drawText [as 别名]
 def paintEvent(self, event):
     QWidget.paintEvent(self, event)
     pmap = self._pixmap
     if pmap.isNull():
         return
     w, h = pmap.width(), pmap.height()
     ow, oh = w, h
     cw, ch = self.rect().width(), self.rect().height()
     scaled, nw, nh = fit_image(w, h, cw, ch)
     if scaled:
         pmap = pmap.scaled(int(nw*pmap.devicePixelRatio()), int(nh*pmap.devicePixelRatio()), Qt.IgnoreAspectRatio,
                 Qt.SmoothTransformation)
     w, h = int(pmap.width()/pmap.devicePixelRatio()), int(pmap.height()/pmap.devicePixelRatio())
     x = int(abs(cw - w)/2.)
     y = int(abs(ch - h)/2.)
     target = QRect(x, y, w, h)
     p = QPainter(self)
     p.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)
     p.drawPixmap(target, pmap)
     if self.draw_border:
         pen = QPen()
         pen.setWidth(self.BORDER_WIDTH)
         p.setPen(pen)
         p.drawRect(target)
     if self.show_size:
         sztgt = target.adjusted(0, 0, 0, -4)
         f = p.font()
         f.setBold(True)
         p.setFont(f)
         sz = u'\u00a0%d x %d\u00a0'%(ow, oh)
         flags = Qt.AlignBottom|Qt.AlignRight|Qt.TextSingleLine
         szrect = p.boundingRect(sztgt, flags, sz)
         p.fillRect(szrect.adjusted(0, 0, 0, 4), QColor(0, 0, 0, 200))
         p.setPen(QPen(QColor(255,255,255)))
         p.drawText(sztgt, flags, sz)
     p.end()
开发者ID:bwhitenb5e,项目名称:calibre,代码行数:38,代码来源:widgets.py


注:本文中的PyQt5.Qt.QPainter.drawText方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。