Widget getting stretched along with the geometry of the layout

70 views Asked by At

I am placing a battery widget in the layout which is getting stretched along with the geometry of the layout which I don't want to be. It has to be of fixed size even though I change the geometry of the layout. And also the widget automatically align itself in the center of the layout even though I changed the position of the layout. here is my code.

import sys
from PySide.QtGui import *
from PySide import QtGui, QtCore
from PySide.QtCore import Signal


class Battery(QProgressBar):

    def __init__(self, *args, **kwargs):
        super(Battery,self).__init__(*args, **kwargs)
        self.setTextVisible(False)
        self.charging = False
        #font = QtGui.QFont("Times", 15, QtGui.QFont.Bold)
        self.setStyleSheet('''
        QProgressBar {
            border: 4px solid black;
            background-color: #c2c2c2;
            margin-top: 12px;
        }
        QProgressBar:horizontal {
            height: 60px;
            width: 120px;
            margin-right: 12px;
        }
        QProgressBar:vertical {
            height: 120px;
            width: 60px;
            margin-left: 12px;
        }
        QProgressBar::chunk {
            background-color: #46EB7D;
            margin: 2px;
            border: 2px solid green;
        }''')

    def setCharging(self, state):
        self.charging = state
        self.repaint()

    def paintEvent(self, event):
        super(Battery,self).paintEvent(event)
        qp = QPainter(self)
        qp.setPen(QtCore.Qt.NoPen); qp.setBrush(QtCore.Qt.black)
        w, h = self.width(), self.height()
        if self.orientation() == QtCore.Qt.Horizontal:
            qp.drawRect(w, 12 + h / 4, -12, h / 2 - 12)
            dx, dy = 0, 12
        else:
            qp.drawRect(12 + w / 4, 0, w / 2 - 12, 12)
            dx, dy = 12, 0

        qp.setPen(self.palette().text().color())
        qp.setFont(QtGui.QFont('Decorative', 12, QtGui.QFont.Bold))
        qp.drawText(self.rect().adjusted(dx, dy, 0, 0), QtCore.Qt.AlignCenter,self.text())
        qp.setPen(QtCore.Qt.NoPen)
        
        if self.charging:
            qp.setBrush(self.parent().palette().window())
            path = QPainterPath()
            if self.orientation() == QtCore.Qt.Horizontal:
                qp.drawRect(0, 0, 12, h)
                path.moveTo(12, h)
                path.lineTo(12, 12 + h / 3)
                path.quadTo(22, 12 + h / 3, 22, 24)
                path.lineTo(22, 14)
                path.lineTo(2, 14)
                path.lineTo(2, 24)
                path.quadTo(2, 12 + h / 3, 12, 12 + h / 3)
                path.moveTo(7, 12); path.lineTo(7, 0)
                path.moveTo(17, 12); path.lineTo(17, 0)
            else:
                qp.drawRect(0, h, w, -12)
                path.moveTo(w, h - 12)
                path.lineTo(12 + w / 3, h - 12)
                path.quadTo(12 + w / 3, h - 22, 24, h - 22)
                path.lineTo(14, h - 22)
                path.lineTo(14, h - 2)
                path.lineTo(24, h - 2)
                path.quadTo(12 + w / 3, h - 2, 12 + w / 3, h - 12)
                path.moveTo(12, h - 7); path.lineTo(0, h - 7)
                path.moveTo(12, h - 17); path.lineTo(0, h - 17)
            
            pen = QPen(qp.brush(), 12, QtCore.Qt.SolidLine, QtCore.Qt.SquareCap, QtCore.Qt.MiterJoin)
            qp.strokePath(path, pen)
            pen.setWidth(4); pen.setColor(QtCore.Qt.black); qp.setPen(pen)
            qp.setBrush(self.palette().window())
            qp.drawPath(path)

class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):
        grid = QGridLayout(self)
        grid.setSpacing(50)
        self.pbar = Battery(value=40)
        grid.addWidget(Battery(value=40), 2, 3)
        self.setGeometry(600, 600, 650, 650)
        self.setWindowTitle('Battery')    
        self.show()       
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

I want my battery image to be placed in a position like below enter image description here

0

There are 0 answers