Python PyQt5.QtGui.QFontMetricsF() Examples

The following are code examples for showing how to use PyQt5.QtGui.QFontMetricsF(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: pyWinContext   Author: VodBox   File: wincontext.py    GNU General Public License v3.0 5 votes vote down vote up
def resizeEvent(self, event):
        if self.path is not None:
            fMetrics = QtGui.QFontMetricsF(QtGui.QFont())
            self.label.setText(fMetrics.elidedText(
                self.path, QtCore.Qt.ElideRight, self.label.width() - 15
            )) 
Example 2
Project: pyWinContext   Author: VodBox   File: wincontext.py    GNU General Public License v3.0 5 votes vote down vote up
def get_file(self):
        file = QFileDialog()
        file.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        file.setFileMode(QFileDialog.ExistingFile)
        self.path = file.getOpenFileUrl(
            filter="Windows Batch File (*.bat)")[0].toLocalFile()
        fMetrics = QtGui.QFontMetricsF(QtGui.QFont())
        self.label.setText(fMetrics.elidedText(
            self.path, QtCore.Qt.ElideRight, self.label.width() - 15
        )) 
Example 3
Project: nanovna-saver   Author: mihtjel   File: NanoVNASaver.py    GNU General Public License v3.0 5 votes vote down vote up
def changeFont(self, font: QtGui.QFont) -> None:
        qf_new = QtGui.QFontMetricsF(font)
        normal_font = QtGui.QFont(font)
        normal_font.setPointSize(8)
        qf_normal = QtGui.QFontMetricsF(normal_font)
        standard_string = "0.123456789 0.123456789 MHz \N{OHM SIGN}"  # Characters we would normally display
        new_width = qf_new.boundingRect(standard_string).width()
        old_width = qf_normal.boundingRect(standard_string).width()
        self.scaleFactor = new_width / old_width
        logger.debug("New font width: %f, normal font: %f, factor: %f", new_width, old_width, self.scaleFactor)
        # TODO: Update all the fixed widths to account for the scaling
        for m in self.markers:
            m.getGroupBox().setFont(font)
            m.setScale(self.scaleFactor) 
Example 4
Project: gr-guiextra   Author: ghostop14   File: compass.py    GNU General Public License v3.0 5 votes vote down vote up
def drawMarkings(self, painter):
        painter.save()
        painter.translate(self.width()/2, self.height()/2)
        scale = min((self.width() - self._margins)/120.0,
                    (self.height() - self._margins)/120.0)
        painter.scale(scale, scale)
        
        font = QFont(self.font())
        #font.setPixelSize(10)
        font.setPixelSize(8)
        metrics = QFontMetricsF(font)
        
        painter.setFont(font)
        # painter.setPen(QPen(Qtc.black))
        painter.setPen(QPen(QColor(self.scaleColor)))
        tickInterval = 5
        i = 0
        while i < 360:
        
            if i % 45 == 0:
                painter.drawLine(0, -40, 0, -50)
                painter.drawText(-metrics.width(self._pointText[i])/2.0, -52, self._pointText[i])
            else:
                painter.drawLine(0, -45, 0, -50)
            
            painter.rotate(tickInterval)
            i += tickInterval
        
        painter.restore() 
Example 5
Project: gr-guiextra   Author: ghostop14   File: dialgauge.py    GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, barColor='blue', backgroundColor='white', fontColor='black', minValue=0, maxValue=100, maxSize=80, 
                 isFloat=False,showValue=False,fixedOrMin=True, parent=None):
        QFrame.__init__(self, parent)

        self.maxSize = maxSize
        super().setMinimumSize(maxSize,maxSize)
        if fixedOrMin:
            super().setMaximumSize(maxSize,maxSize)
        
        self.backgroundColor = backgroundColor
        self.barColor = barColor
        self.fontColor = fontColor
        self.isFloat = isFloat
        self.showValue = showValue
        
        self.value = minValue
        
        self.minValue = minValue
        self.maxValue = maxValue
        
        self.textfont = QFont(self.font())
        #font.setPixelSize(10)
        self.textfont.setPixelSize(16)
        self.metrics = QFontMetricsF(self.textfont)
            
        self.startAngle = 0.0
        self.endAngle = 360.0
        self.degScaler = 16.0 # The span angle must be specified in 1/16 of a degree units
        self.penWidth = max(int(0.1 * maxSize),6)
        self.halfPenWidth = int(self.penWidth / 2) 
Example 6
Project: gr-guiextra   Author: ghostop14   File: toggleswitch.py    GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, lbl='', onColor='green', offColor='red', initialState=False, maxSize=50, position=1, parent=None, callback=None, alignment=1, valignment=1):
        QFrame.__init__(self, parent)
        self.numberControl = ToggleSwitch(onColor, offColor, initialState, maxSize, parent, callback)
        
        if position < 3:
            layout =  QVBoxLayout()
        else:
            layout = QHBoxLayout()
            
        self.lbl = lbl
        self.lblcontrol = QLabel(lbl, self)
        
        if position == 3: # left of switch
            self.lblcontrol.setAlignment(Qtc.AlignRight)
        elif position == 4: # right of switch
            self.lblcontrol.setAlignment(Qtc.AlignLeft)
        else:
            # Above or below
            self.lblcontrol.setAlignment(Qtc.AlignCenter)

        # add top or left
        if len(lbl) > 0:
            if position == 1 or position == 3:
                layout.addWidget(self.lblcontrol)

        layout.addWidget(self.numberControl)
        
        # Add bottom or right
        if len(lbl) > 0:
            if position == 2 or position == 4:
                layout.addWidget(self.lblcontrol)
                
        if alignment == 1:        
            halign = Qtc.AlignCenter
        elif alignment == 2:
            halign = Qtc.AlignLeft
        else:
            halign = Qtc.AlignRight

        if valignment == 1:
            valign = Qtc.AlignVCenter
        elif valignment == 2:
            valign = Qtc.AlignTop
        else:
            valign = Qtc.AlignBottom
            
        layout.setAlignment(halign | valign)
        # layout.setAlignment(Qtc.AlignCenter | Qtc.AlignVCenter)
        self.setLayout(layout)
        
        textfont = self.lblcontrol.font()
        metrics = QFontMetricsF(textfont)
        
        maxWidth = max( (maxSize+4),(maxSize*2 + metrics.width(lbl)) )
        maxHeight = max( (maxSize/2+4),(maxSize/2 + metrics.height()+2) )
        #print('Min size: ' + str(maxWidth) + " x " + str(maxHeight))
        
        self.setMinimumSize(int(maxWidth), int(maxHeight))

        self.show() 
Example 7
Project: gr-guiextra   Author: ghostop14   File: ledindicator.py    GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, lbl='', onColor='green', offColor='red', initialState=False, maxSize=80, position=1, alignment=1, valignment=1, parent=None):
        QFrame.__init__(self, parent)
        self.numberControl = LEDIndicator(onColor, offColor, initialState, maxSize, parent)
        
        if position < 3:
            layout =  QVBoxLayout()
        else:
            layout = QHBoxLayout()
            
        if len(lbl) == 0:
            lbl = " "
        self.lbl = lbl
        self.lblcontrol = QLabel(lbl, self)
        self.lblcontrol.setAlignment(Qtc.AlignCenter)

        # add top or left
        if len(lbl) > 0:
            if position == 1 or position == 3:
                layout.addWidget(self.lblcontrol)
        else:
            self.hasLabel = False

        layout.addWidget(self.numberControl)
        
        # Add bottom or right
        if len(lbl) > 0:
            if position == 2 or position == 4:
                layout.addWidget(self.lblcontrol)
                
        if alignment == 1:        
            halign = Qtc.AlignCenter
        elif alignment == 2:
            halign = Qtc.AlignLeft
        else:
            halign = Qtc.AlignRight

        if valignment == 1:
            valign = Qtc.AlignVCenter
        elif valignment == 2:
            valign = Qtc.AlignTop
        else:
            valign = Qtc.AlignBottom
            
        layout.setAlignment(halign | valign)
        self.setLayout(layout)
        
        if (len(lbl) > 0):
            textfont = self.lblcontrol.font()
            metrics = QFontMetricsF(textfont)
            
            maxWidth = max( (maxSize+30),(maxSize + metrics.width(lbl)+4) )
            maxHeight = max( (maxSize+35),(maxSize + metrics.height()+2) )
            self.setMinimumSize(maxWidth, maxHeight)
        else:
            self.setMinimumSize(maxSize+2, maxSize+2)  

        self.show()