QtGui.QPen
The QtGui.QPen is an elementary graphics object. It is used to draw lines, curves and outlines of rectangles, ellipses, polygons, or other shapes.
#!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial In this example we draw 6 lines using
different pen styles. 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.setGeometry(300, 300, 280, 270)
self.setWindowTitle('Pen styles')
self.show() def paintEvent(self, e): qp = QtGui.QPainter()
qp.begin(self)
self.drawLines(qp)
qp.end() def drawLines(self, qp): pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine) qp.setPen(pen)
qp.drawLine(20, 40, 250, 40) pen.setStyle(QtCore.Qt.DashLine)
qp.setPen(pen)
qp.drawLine(20, 80, 250, 80) pen.setStyle(QtCore.Qt.DashDotLine)
qp.setPen(pen)
qp.drawLine(20, 120, 250, 120) pen.setStyle(QtCore.Qt.DotLine)
qp.setPen(pen)
qp.drawLine(20, 160, 250, 160) pen.setStyle(QtCore.Qt.DashDotDotLine)
qp.setPen(pen)
qp.drawLine(20, 200, 250, 200) pen.setStyle(QtCore.Qt.CustomDashLine)
pen.setDashPattern([1, 4, 5, 4])
qp.setPen(pen)
qp.drawLine(20, 240, 250, 240) def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()
In our example, we draw six lines. The lines are drawn in six different pen styles. There are five predefined pen styles. We can create also custom pen styles. The last line is drawn using a custom pen style.
pen = QtGui.QPen(QtCore.Qt.black, 2, QtCore.Qt.SolidLine)
We create a QtGui.QPen object. The colour is black. The width is set to 2 pixels so that we can see the differences between the pen styles. The QtCore.Qt.SolidLine is one of the predefined pen styles.
pen.setStyle(QtCore.Qt.CustomDashLine)
pen.setDashPattern([1, 4, 5, 4])
qp.setPen(pen)
Here we define a custom pen style. We set a QtCore.Qt.CustomDashLine pen style and call thesetDashPattern() method. The list of numbers defines a style. There must be an even number of numbers. Odd numbers define a dash, even numbers space. The greater the number, the greater the space or the dash. Our pattern is 1px dash, 4px space, 5px dash, 4px space etc.
Figure: Pen styles
QtGui.QPen的更多相关文章
- 用 eric6 与 PyQt5 实现python的极速GUI编程(系列03)---- Drawing(绘图)(3)-- 画线
[概览] 本文实现如下的程序:(在窗体中绘画出各种不同风格的线条) 主要步骤如下: 1.在eric6中新建项目,新建窗体 2.(自动打开)进入PyQt5 Desinger,编辑图形界面,保存 3.回到 ...
- ZetCode PyQt4 tutorial custom widget
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- ZetCode PyQt4 tutorial basic painting
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...
- Burning widget
This is a widget that we can see in Nero, K3B, or other CD/DVD burning software. #!/usr/bin/python # ...
- Python游戏引擎开发(七):绘制矢量图
今天来完毕绘制矢量图形. 没有读过前几章的同学,请先阅读前几章: Python游戏引擎开发(一):序 Python游戏引擎开发(二):创建窗体以及重绘界面 Python游戏引擎开发(三):显示图片 P ...
- 读Pyqt4教程,带你入门Pyqt4 _013
你是否曾经看着应用程序并思考特定的GUI项是如何产生的?大概每位程序员都这样过.然后你能看到你喜欢的GUI库提供的一系列窗口组件,但是你无法找到它.工具包通常仅仅提供最常用的窗口组件,比如按钮.文本组 ...
- 读Pyqt4教程,带你入门Pyqt4 _012
颜色 颜色是指一个代表红(Red).绿(Green).蓝(Blue)(RGB)强度值组合的对象,有效的RGB值在0~255之间.我们可以用多种方式定义颜色,最常用的是RGB十进制或者十六进制值.也可以 ...
- 100行Python代码实现一款高精度免费OCR工具
近期Github开源了一款基于Python开发.名为 Textshot 的截图工具,刚开源不到半个月已经500+Star. 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语 ...
- 第15.47节、PyQt显示部件:QGraphicsView图形视图和QGraphicsScene图形场景简介及应用案例
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 Designer中的Graphics V ...
随机推荐
- [BZOJ3140][HNOI2013]消毒(二分图最小点覆盖)
3140: [Hnoi2013]消毒 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1621 Solved: 676[Submit][Status] ...
- 零配置文件搭建SpringMvc
零配置文件搭建SpringMvc SpringMvc 流程原理 (1)用户发送请求至前端控制器DispatcherServlet:(2) DispatcherServlet收到请求后,调用Handle ...
- [BZOJ1005](HNOI 2008)明明的烦恼
Description 自从明明学了树的结构,就对奇怪的树产生了兴趣...... 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为 ...
- 检测使用内存memory_get_usage,执行时间microtime
最近经常用一些扩展,适当比较所占内存,还有一些扩展执行时间长,检测一下每步的执行时间,可以加以修正调整一下源码 查看运行时间 microtime() #返回当前 Unix 时间戳和微秒数. echo ...
- 28个Java常用的工具类
源码下载:http://pan.baidu.com/s/1pJLSczD Base64.javaBase64DecodingException.javaCConst.javaCharTools.jav ...
- STM32 USB VBUS 监控
OTG_FS general core configuration register (OTG_FS_GCCFG) Bit 21 NOVBUSSENS: VBUS sensing disable op ...
- Your first NHibernate based application
Welcome to NHibernate If you're reading this, we assume that you've just downloaded NHibernate and w ...
- KVM-Introduce
相信非常多的人对虚拟机并不陌生,眼下也有非常多优秀的虚拟机软件,比如:VMware, VirtualBox, Xen, KVM等.而本文的主要内容是介绍KVM. KVM: Kernel Based V ...
- Labeled Faces in the Wild 人脸识别数据集 部分测试数据
development test set Note: images displayed are original (non-aligned/funneled) images. match pairs ...
- 11.线程通信CountDownLatch
package demo2; import java.util.concurrent.CountDownLatch; /** * Created by liudan on 2017/7/27. */ ...