Drawing text

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

时间: 2024-12-12 19:38:04

Drawing text的相关文章

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

for (int i = 0; i < 6; i++) { g5.TextRenderingHint = (System.Drawing.Text.TextRenderingHint)i; string txt; int font_sz = 25; txt = "Static 测试 "; switch ((System.Drawing.Text.TextRenderingHint)i) { case System.Drawing.Text.TextRenderingHint.Sy

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

1. [代码]CircleTextDemo.java     import java.awt.*;import java.awt.event.*;import java.awt.geom.*; /** * A demo class that illustrates drawing text * along the outside of a circle. */public class CircleTextDemo extends Canvas {    Frame myframe;    Tex

类库探源——System.Drawing

一.System.Drawing 命名空间简述 System.Drawing 命名空间提供访问 GDI+ 的基本功能,更高级的功能在 System.Drawing.Drawing2D,System.Drawing.Imaging 和 System.Drawing.Text 命名空间 程序集: System.Drawing.dll 二.System.Drawing.Image 简述 Image 类:为源自 Bitmap 和 Metafile 的类提供功能的抽象基类 命名空间: System.Dra

生成缩略图时报GDI+中发生一般性错误

最近由于业务需要要写一个生成缩略图并能设置图片质量的功能,本来这是一件so easy的事,以前也干过,可是却遇到了问题.话不多说先看代码 /// <summary> /// 等比生成缩略图 /// </summary> /// <param name="originalImagePath">源图路径(物理路径)</param> /// <param name="thumbnailPath">缩略图路径(物理

C# JackLib系列之字体使用

字体的使用一般我们都是使用系统字体,这样比较方便,直接 Font font=new Font("微软雅黑",16f,FontStyle.Bold); 但是当我们用到一个系统没有的字体库时,这个方法就不好用了,因此我们可以采用动态加载字体文件的方式或者直接把字体打包到我们的程序集里当作资源来使用: 下面我们来看一下怎么用: 我封装了一个类,大家可以直接使用,如果有不好的地方,欢迎大家指正. using System; using System.Collections.Generic; u

Canvas HTML5

Canvas Examples 一个canvas是在html页面上规则的区域. 默认的,一个canvas没有边框和内容 <canvas id="myCanvas" width="200" height="100"></canvas> 基本步骤 <script> var canvas = document.getElementById("myCanvas");//发现canvas元素 var

Kean专题 内容Jig 在绘图界面动态显示坐标

二.在绘图界面动态显示坐标 原文转载自:http://through-the-interface.typepad.com/through_the_interface/jigs/(该口已无法访问) 可访问转载入口:http://bbs.mjtd.com/thread-75618-1-1.html(转载自明镜通道by雪山飞狐_lzh) 原kean博客已经无法看到,故转载明经通道 雪山飞狐_lzh 老师整理内容 1.kean博客原文翻译 December 12, 2008 Drawing text p

GDI+编程小结

GDI+(Graphics Device Interface Plus图形设备接口加)是Windows XP和Windows Server 2003操作系统的子系统,也是.NET框架的重要组成部分,负责在屏幕和打印机上绘制图形图像和显示信息. GDI+不但在功能上比GDI 要强大很多,而且在代码编写方面也更简单,因此会很快成为Windows图形图像程序开发的首选. 一.              GDI+的特点和新增功能 GDI+与GDI一样,都具有设备无关性.应用程序的程序员可利用GDI+这样

【干货】国外程序员整理的 C++ 资源大全【转】

来自 https://github.com/fffaraz/awesome-cpp A curated list of awesome C/C++ frameworks, libraries, resources, and shiny things. Inspired by awesome-... stuff Standard Libraries C++ Standard Library - including STL Containers, STL Aglorithm, STL Functio