We begin with drawing some Unicode text on the client area of a window.

#!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial In this example, we draw text in Russian azbuka. author: Jan Bodnar
website: zetcode.com
last edited: September 2011
""" import sys
from PyQt4 import QtGui, QtCore class Example(QtGui.QWidget): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): self.text = u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430\
\u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\u0442\u043e\u0439: \n\
\u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430' self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Draw text')
self.show() def paintEvent(self, event): qp = QtGui.QPainter()
qp.begin(self)
self.drawText(event, qp)
qp.end() def drawText(self, event, qp): qp.setPen(QtGui.QColor(168, 34, 3))
qp.setFont(QtGui.QFont('Decorative', 10))
qp.drawText(event.rect(), QtCore.Qt.AlignCenter, self.text) def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()

In our example, we draw some text in Azbuka. The text is vertically and horizontally aligned.

def paintEvent(self, event):
...

Drawing is done within the paint event.

qp = QtGui.QPainter()
qp.begin(self)
self.drawText(event, qp)
qp.end()

The QtGui.QPainter class is responsible for all the low-level painting. All the painting methods go between begin() and end() methods. The actual painting is delegated to the drawText() method.

qp.setPen(QtGui.QColor(168, 34, 3))
qp.setFont(QtGui.QFont('Decorative', 10))

Here we define a pen and a font which are used to draw the text.

qp.drawText(event.rect(), QtCore.Qt.AlignCenter, self.text)

The drawText() method draws text on the window. The rect() method of the paint event returns the rectangle that needs to be updated.

Figure: Drawing text

Drawing text的更多相关文章

  1. System.Drawing.Text.TextRenderingHint 的几种效果

    ; i < ; i++) { g5.TextRenderingHint = (System.Drawing.Text.TextRenderingHint)i; string txt; ; txt ...

  2. mono+jexus 验证码不显示:System.Drawing

    System.ArgumentException The requested FontFamily could not be found [GDI+ status: FontFamilyNotFoun ...

  3. Cocoa Drawing

    Graphics Contexts Graphics contexts are a fundamental part of the drawing infrastructure in Cocoa ap ...

  4. 类库探源——System.Drawing

    一.System.Drawing 命名空间简述 System.Drawing 命名空间提供访问 GDI+ 的基本功能,更高级的功能在 System.Drawing.Drawing2D,System.D ...

  5. Quartz2D Text

    [Quartz2D Text] Quartz 2D provides a limited, low-level interface for drawing text encoded in the Ma ...

  6. Java 绘制环形的文字 (Circle Text Demo)

    1. [代码]CircleTextDemo.java     import java.awt.*;import java.awt.event.*;import java.awt.geom.*; /** ...

  7. System.Drawing.Graphics.cs

    ylbtech-System.Drawing.Graphics.cs 1.程序集 System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKey ...

  8. asp.net中ashx生成验证码代码放在Linux(centos)主机上访问时无法显示问题

    最近有个项目加入了验证码功能,就从自己博客以前的代码中找到直接使用,直接访问验证码页面报错如下: 源代码:asp.net中使用一般处理程序生成验证码 Application Exception Sys ...

  9. ASP.NET图形验证码的生成

    效果: 调用方法: int[] r = QAPI.VerifImage.RandomList();//取得随机数种子列 );//产生验证码字符 pictureBox1.Image = QAPI.Ver ...

随机推荐

  1. [QSCOJ39]喵哈哈村的代码传说 第五章 找规律

    题目大意: 给你n堆排,两人轮流对其中一堆牌进行以下操作之一: 1.从这堆牌中取出任意数量的牌: 2.将这这堆牌分为任意大小的3堆牌. 不能操作者负. 问先手是否有必胜策略. 思路: 尝试构造sg函数 ...

  2. bzoj 1115: [POI2009]石子游戏Kam -- 博弈论

    1115: [POI2009]石子游戏Kam Time Limit: 10 Sec  Memory Limit: 162 MB Description 有N堆石子,除了第一堆外,每堆石子个数都不少于前 ...

  3. 实用在线小工具 -- JS代码压缩工具

        实用在线小工具 -- JS代码压缩工具 将JS代码进行压缩可以减少内存占用,下面链接是一个在线JS代码压缩工具,它将多余的空格和换行符压缩了. JS代码压缩工具链接:http://jspack ...

  4. Ubuntu14.04环境下配置TFTP服务器

    <<<<<<<<<<<<<<<<<<<<<<<<< ...

  5. [转].net reactor 学习系列(三)---.net reactor代码自动操作相关保护功能

    接上篇,上篇已经学习了界面的各种功能以及各种配置,这篇准备学习下代码控制许可证. 代码控制许可证的意思就是软件经过.net reactor保护后,到期时客户端就需要购买许可证,这时软件开发商就需要生成 ...

  6. Android PopupWindow做的分享界面

    package com.tq.mbaexam.view; import java.util.ArrayList; import java.util.LinkedHashMap; import java ...

  7. 【转 :Hibernate 缓存机制】

    转自:http://www.cnblogs.com/wean/archive/2012/05/16/2502724.html Hibernate 缓存机制 一.why(为什么要用Hibernate缓存 ...

  8. Silverlight:《Pro Silverlight5》读书笔记 之 Dependency Properties And Routed Event

    Dependency Properties And Routed Event Dependency Properties Dynamic Value Resolution As you’ve alre ...

  9. PHP:面向对象学习笔记,重点模拟Mixin(掺入)

    背景 相对于Python.Node和Ruby来说PHP算是一门容易学习和使用的语言,因为这个特点也使其成为WEB开发领域的佼佼者,本文记录一下我对PHP面向对象部分的学习笔记. 先来一个复杂的例子:M ...

  10. Starling 2D框架简介(一)

    本系列是对Introducing Starling pdf的翻译,下文是对adobe开发人员中心的一片日志的转载,地址为http://www.adobe.com/cn/devnet/flashplay ...