zoukankan      html  css  js  c++  java
  • 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'u041bu0435u0432 u041du0438u043au043eu043bu0430
    u0435u0432u0438u0447 u0422u043eu043bu0441u0442u043eu0439: 
    
    u0410u043du043du0430 u041au0430u0440u0435u043du0438u043du0430'
    
            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.

    Drawing textFigure: Drawing text

  • 相关阅读:
    java环境变量配置(转)
    【Android】SlidingMenu属性详解(转)
    android.intent.action.MAIN 与 android.intent.category.LAUNCHER 的验证理解 (转)
    实现Activity刷新(转)
    测试服务API的_苏飞开发助手_使用说明
    在getView方法产生给用户item的视图以及数据
    pl/sql developer 登陆提示ORA-12514(转)
    tnsnames.ora存放路径
    一个较为复杂的布局例子
    Android ImageView图片自适应 (转)
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4436934.html
Copyright © 2011-2022 走看看