ZetCode PyQt4 tutorial custom widget

简介: #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we create a custom widget.
#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In this example, we create a custom widget.

author: Jan Bodnar
website: zetcode.com 
last edited: October 2011
"""

import sys
from PyQt4 import QtGui, QtCore


class Communicate(QtCore.QObject):
    
    updateBW = QtCore.pyqtSignal(int)


# The burning widget it based on the QtGui.QWidget widget.
class BurningWidget(QtGui.QWidget):
  
    def __init__(self):      
        super(BurningWidget, self).__init__()
        
        self.initUI()
        
    def initUI(self):
        
        # We change the minimum size (height) of the widget. The default value is a bit small for us.
        self.setMinimumSize(1, 30)
        self.value = 75
        self.num = [75, 150, 225, 300, 375, 450, 525, 600, 675]


    def setValue(self, value):

        self.value = value


    def paintEvent(self, e):
      
        qp = QtGui.QPainter()
        qp.begin(self)
        self.drawWidget(qp)
        qp.end()
      
      
    def drawWidget(self, qp):
      
        # We use a smaller font than the default one. This better suits our needs.
        font = QtGui.QFont('Serif', 7, QtGui.QFont.Light)
        qp.setFont(font)

        # We draw the widget dynamically. The greater is the window, the greater is the burning widget and vice versa. That is why we must calculate the size of the widget onto which we draw the custom widget. The till parameter determines the total size to be drawn. This value comes from the slider widget. It is a proportion of the whole area. The full parameter determines the point where we begin to draw in red colour. Notice the use of floating point arithmetics to achieve greater precision in drawing.
        # The actual drawing consists of three steps. We draw the yellow or the red and yellow rectangle. Then we draw the vertical lines which divide the widget into several parts. Finally, we draw the numbers which indicate the capacity of the medium.
        size = self.size()
        w = size.width()
        h = size.height()

        step = int(round(w / 10.0))

        till = int(((w / 750.0) * self.value))
        full = int(((w / 750.0) * 700))

        if self.value >= 700:
        
            qp.setPen(QtGui.QColor(255, 255, 255))
            qp.setBrush(QtGui.QColor(255, 255, 184))
            qp.drawRect(0, 0, full, h)
            qp.setPen(QtGui.QColor(255, 175, 175))
            qp.setBrush(QtGui.QColor(255, 175, 175))
            qp.drawRect(full, 0, till-full, h)
            
        else:
            qp.setPen(QtGui.QColor(255, 255, 255))
            qp.setBrush(QtGui.QColor(255, 255, 184))
            qp.drawRect(0, 0, till, h)


        pen = QtGui.QPen(QtGui.QColor(20, 20, 20), 1, 
            QtCore.Qt.SolidLine)
            
        qp.setPen(pen)
        qp.setBrush(QtCore.Qt.NoBrush)
        qp.drawRect(0, 0, w-1, h-1)

        j = 0

        for i in range(step, 10*step, step):
          
            qp.drawLine(i, 0, i, 5)
            # We use font metrics to draw the text. We must know the width of the text in order to center it around the vertical line.
            metrics = qp.fontMetrics()
            fw = metrics.width(str(self.num[j]))
            qp.drawText(i-fw/2, h/2, str(self.num[j]))
            j = j + 1
            

class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):      

        sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
        sld.setFocusPolicy(QtCore.Qt.NoFocus)
        sld.setRange(1, 750)
        sld.setValue(75)
        sld.setGeometry(30, 40, 150, 30)

        self.c = Communicate()        
        self.wid = BurningWidget()
        self.c.updateBW[int].connect(self.wid.setValue)

        sld.valueChanged[int].connect(self.changeValue)
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.wid)
        vbox = QtGui.QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)
        self.setLayout(vbox)
        
        self.setGeometry(300, 300, 390, 210)
        self.setWindowTitle('Burning widget')
        self.show()
        
    # When we move the slider, the changeValue() method is called. Inside the method, we send a custom updateBW signal with a parameter. The parameter is the current value of the slider. The value is later used to calculate the capacity of the Burning widget to be drawn. The custom widget is then repainted.
    def changeValue(self, value):
             
        self.c.updateBW.emit(value)        
        self.wid.repaint()
        
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

 

目录
打赏
0
0
0
0
12
分享
相关文章
7月5日云栖精选夜读:物联网技术在农业领域的应用
近年来,世界农业物联网技术不断发展,在农业物联网感知技术、数据传输技术、智能处理技术等方面取得了很大的进展,随着现代农业自动化需求的不断增长和信息技术的发展,农业物联网在物理感知、数据传输、智能处理、应用服务等领域取得了重要进展。
4403 0
阿里云EMR计算速度提升2.2倍 连续两年打破大数据领域最难竞赛世界纪录!
4月26日,大数据领域权威竞赛TPC-DS公布了最新结果,阿里云作为全球唯一入选的云计算公司获得第一。值得一提的是,去年阿里云EMR首次打破该竞赛纪录,成为全球首个通过TPC认证的公共云产品。今年在这一基础上,EMR的计算速度提升了2.2倍,连续两年打破了这项大数据领域最难竞赛的世界纪录。
阿里云EMR计算速度提升2.2倍 连续两年打破大数据领域最难竞赛世界纪录!
在阿里做双职工是怎样的体验
昨天内网里有一对双职工(指在阿里上班的夫妻俩)蚂蚁金服小二行洋&旭颖,晒出了他们的一组“follow me”——
5396 0
阿里云容器服务简介
容器服务是一项高性能可扩展的容器管理服务,支持在一组阿里云云服务器上通过 Docker容器来部署或编排应用。用户不再需要安装、运维、扩展自己的集群管理基础设施,而是可以直接通过阿里云控制台图形化界面或API进行容器操作和生命周期管理。
9767 0
基于DTS+Tablestore的海量订单系统架构设计
DTS支持MySQL同步Tablestore Beta版上线,合力打造完善的订单系统。 本文主要介绍一套基于DTS与Tablestore实现一套完善的订单系统架构。实时订单数据主要针对用户侧的实时生产与修改,实例订单数据则是基于数据同步服务DTS,全、增量订阅TP库中的订单数据,从而保证Tablestore中数据与TP库数据的最终一致性。异步同步的方式不可避免的存在延时,但历史订单库在实时性上要求会适当放宽,但其派生出来的数据在服务能力与功能扩展上得到了极大的提升,尤其是Tablestore这种分布式服务能力强、下游计算生态丰富的NoSQL存储服务。
14633 0
基于DTS+Tablestore的海量订单系统架构设计
用uni-app开发一个名为汉兜的游戏
虽然我是带队,但我希望尽可能让队员们自己去完成游戏的代码部分,我负责出出主意,提供一些美术,玩法创意上的支持。其实一开始大家头脑风暴组队领游戏创意的时候,汉兜这个游戏一直没人领,不得不说,不知道叫啥名队的小伙伴执行力很强,连给游戏起名字都很快,一点都不拖泥带水。
2078 0
用uni-app开发一个名为汉兜的游戏
基于OSS作为存储实现加速访问和加速上传的方案实现
本文通过实现OSS加速的两种方式CDN加速OSS和OSS传输加速来介绍OSS的加速的配置实现方式
7994 0
基于OSS作为存储实现加速访问和加速上传的方案实现
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等