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

最新文章

  1. VS2012完全卸载与VS2013安装
  2. Combination Sum
  3. C# 网络与Cmd命令
  4. hdu 2899(数学基础+二分)
  5. 删除mysql的root用户恢复方法
  6. Java [Leetcode 110]Balanced Binary Tree
  7. CentOS添加中科大、163 yum源
  8. css书写顺序和常用命名推荐
  9. opencv开源库
  10. 【剑指offer】第一个字符只出现一次
  11. assert的基本用法
  12. 10.Java 加解密技术系列之 DH
  13. PipeHttp 测试工具使用
  14. 使用C++的string实现高精度加法运算
  15. ubuntu下eclipse新建项目没有java project的解决办法
  16. 小米平板6.0以上系统如何不用Root激活Xposed框架的步骤
  17. MySql思维导图
  18. 使用Tenorshare iCareFone for mac为iPhone做系统修复
  19. 新的 Centos 服务器初始化配置
  20. 【BZOJ1004】【HNOI2008】Cards 群论 置换 burnside引理 背包DP

热门文章

  1. 【Floyd算法】Gym - 101572I - Import Spaghetti
  2. 【二分答案】【DFS】【分类讨论】Gym - 100851F - Froggy Ford
  3. 关于iis6.0远程溢出漏洞
  4. [HihoCoder1169]猜单词
  5. 扩展gcd codevs 1200 同余方程
  6. HDU 2896 病毒侵袭 【AC自动机】
  7. 【redis】redis五大类 用法 【转载:https://www.cnblogs.com/yanan7890/p/6617305.html】
  8. ORACLE的VARCHAR2是字节还是字符
  9. python 实现Hadoop的partitioner和二次排序
  10. Openshift 和Harbor的集成