The QtGui.QBrush is an elementary graphics object. It is used to paint the background of graphics shapes, such as rectangles, ellipses, or polygons. A brush can be of three different types: a predefined brush, a gradient, or a texture pattern.

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. ZetCode PyQt4 tutorial
  6.  
  7. This example draws 9 rectangles in different
  8. brush styles.
  9.  
  10. author: Jan Bodnar
  11. website: zetcode.com
  12. last edited: September 2011
  13. """
  14.  
  15. import sys
  16. from PyQt4 import QtGui, QtCore
  17.  
  18. class Example(QtGui.QWidget):
  19.  
  20. def __init__(self):
  21. super(Example, self).__init__()
  22.  
  23. self.initUI()
  24.  
  25. def initUI(self):
  26.  
  27. self.setGeometry(300, 300, 355, 280)
  28. self.setWindowTitle('Brushes')
  29. self.show()
  30.  
  31. def paintEvent(self, e):
  32.  
  33. qp = QtGui.QPainter()
  34. qp.begin(self)
  35. self.drawBrushes(qp)
  36. qp.end()
  37.  
  38. def drawBrushes(self, qp):
  39.  
  40. brush = QtGui.QBrush(QtCore.Qt.SolidPattern)
  41. qp.setBrush(brush)
  42. qp.drawRect(10, 15, 90, 60)
  43.  
  44. brush.setStyle(QtCore.Qt.Dense1Pattern)
  45. qp.setBrush(brush)
  46. qp.drawRect(130, 15, 90, 60)
  47.  
  48. brush.setStyle(QtCore.Qt.Dense2Pattern)
  49. qp.setBrush(brush)
  50. qp.drawRect(250, 15, 90, 60)
  51.  
  52. brush.setStyle(QtCore.Qt.Dense3Pattern)
  53. qp.setBrush(brush)
  54. qp.drawRect(10, 105, 90, 60)
  55.  
  56. brush.setStyle(QtCore.Qt.DiagCrossPattern)
  57. qp.setBrush(brush)
  58. qp.drawRect(10, 105, 90, 60)
  59.  
  60. brush.setStyle(QtCore.Qt.Dense5Pattern)
  61. qp.setBrush(brush)
  62. qp.drawRect(130, 105, 90, 60)
  63.  
  64. brush.setStyle(QtCore.Qt.Dense6Pattern)
  65. qp.setBrush(brush)
  66. qp.drawRect(250, 105, 90, 60)
  67.  
  68. brush.setStyle(QtCore.Qt.HorPattern)
  69. qp.setBrush(brush)
  70. qp.drawRect(10, 195, 90, 60)
  71.  
  72. brush.setStyle(QtCore.Qt.VerPattern)
  73. qp.setBrush(brush)
  74. qp.drawRect(130, 195, 90, 60)
  75.  
  76. brush.setStyle(QtCore.Qt.BDiagPattern)
  77. qp.setBrush(brush)
  78. qp.drawRect(250, 195, 90, 60)
  79.  
  80. def main():
  81.  
  82. app = QtGui.QApplication(sys.argv)
  83. ex = Example()
  84. sys.exit(app.exec_())
  85.  
  86. if __name__ == '__main__':
  87. main()

In our example, we draw nine different rectangles.

  1. brush = QtGui.QBrush(QtCore.Qt.SolidPattern)
  2. qp.setBrush(brush)
  3. qp.drawRect(10, 15, 90, 60)

We define a brush object. We set it to the painter object and draw the rectangle by calling thedrawRect() method.

Figure: Brushes

QtGui.QBrush的更多相关文章

  1. 【转载】Pyqt 编写的俄罗斯方块

    #!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function from __future__ ...

  2. Python win32api提取exe图标icon

    转载地址: http://blog.csdn.net/gumanren/article/details/6129416 代码如下: # -*- coding: utf-8 -*- import sys ...

  3. Pyqt 窗体间传值

    窗体间传值网上有好多方法,比如新建文件,先将子类窗体的数据传到文件中,父窗体读取文件.  Signal&Slot机制进行传值 等等 在这里,我们就举个采用apply方法:Signal& ...

  4. Pyqt 打包资源文件

    用打包工具将做好的Pyqt程序打包成exe后发现引用的资源图片都显示不了? 是否遇到了和我一样的问题呢.google之后找到了方法,一种方法是在程序中引用外部资源,另外一种方法是将资源文件转换为py文 ...

  5. Pyqt 设置 背景颜色和背景图片、 QPalette 调色板 与QPainter 画板区别 、 不规则图片

    设置 背景颜色和背景图片 首先设置autoFillBackground属性为真然后定义一个QPalette对象设置QPalette对象的背景属性(颜色或图片)最后设置QWidget对象的Palette ...

  6. pyqt4制作透明无边框窗体

    用PyQt做了一个无边框登陆窗口,效果如下: 下面是代码: # -*- coding: utf-8 -*- from PyQt4 import QtGui ,Qt ,QtCore image=QtGu ...

  7. pyqt记录内容(音乐播放器)

    #这是UI文件 # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'AudioPlayerDia ...

  8. PyQt:个性化登录界面模仿QQ登录

    写在前面 写了一个登录界面的demo,类似QQ的,写的自己喜欢的样式,贴一下代码,先上效果,如下 陈述 PyQt5+Python3.5.2 login.py是里登录的主界面loginWnd类,Head ...

  9. pyqt5_eric6_Qt desinger

    麦子学院视频教程day1 1.创建pushbutton 绑定信号和槽 Ui_mainWindow.py 1 from PyQt5 import QtCore, QtGui, QtWidgets cla ...

随机推荐

  1. 解决关于stack溢出的问题

    开发中经常遇到: 前端遇到Uncaught RangeError: Maximum call stack size exceeded错误 后台遇到java.lang.OutOfMemoryError: ...

  2. [COGS2427][HZOI 2016]seq

    [COGS2427][HZOI 2016]seq 题目大意: 一个长度为\(n(n\le10^6)\)的序列,\(q(q\le10^6)\)次操作,每次将所有\(a\)变成\(b\),求最后的序列. ...

  3. 高斯消元法求解异或方程组: cojs.tk 539.//BZOJ 1770 牛棚的灯

    高斯消元求解异或方程组: 比较不错的一篇文章:http://blog.sina.com.cn/s/blog_51cea4040100g7hl.html cojs.tk  539. 牛棚的灯 ★★☆   ...

  4. 基础知识(09) -- Spring 概述

    Spring概述-------------------------------------------------------------------------主要内容: 1.Spring是什么 2 ...

  5. python开发_random

    和java中的random()函数一样,在python中也有类似的模块random,即随机数 下面是我做的demo 运行效果: ==================================== ...

  6. 【原】Project configuration is not up-to-date with pom.xml

    导入一个Maven项目之后发现有一个如下的错误: Project configuration is not up-to-date with pom.xml. Run project configura ...

  7. How to open a web site with the default web browser in a NEW window

    http://delphi.about.com/cs/adptips2004/a/bltip0504_4.htm When using ShellExecute (as explained in th ...

  8. WICED -- Wireless Internet Connectivity for Embedded Devices

    Broadcom's Wireless Internet Connectivity for Embedded Devices (WICED™) platform (pronounced "w ...

  9. .Net4.0并行库介绍——Task

    Task和ThreadPool的功能类似,可以用来创建一些轻量级的并行任务.对于将一个任务放进线程池     ThreadPool.QueueUserWorkItem(A); 这段代码用Task来实现 ...

  10. h264 封装 RTMP中FLV数据的解析 rtmp协议简单解析以及用其发送h264的flv文件

    一个完整的多媒体文件是由音频和视频2部分组成的.H264.Xvid等就是视频编码格式,MP3.AAC等就是音频编码格式.字幕文件只是其中附带部分. 把视频编码和音频编码打包成一个完整的多媒体文件,可以 ...