栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将标签字体大小与PyQt中的布局同步

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将标签字体大小与PyQt中的布局同步

以下是

QLabel
从此处发布的解决方案衍生出来的解决方案:https : //forum.qt.io/topic/36088/automatically-scale-
text-in-qlabels/5

这包括

resizeEvent
方法的重新实现,其中
QLabel
根据的大小更新的字体大小
contentRect
。请注意,必须
sizePolicy
将Qlabel的设置
Ignored
为此才能正常工作。

import sysfrom PyQt4 import QtGuiclass myQLabel(QtGui.QLabel):    def __init__(self, *args, **kargs):        super(myQLabel, self).__init__(*args, **kargs)        self.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Ignored, QtGui.QSizePolicy.Ignored))        self.setMinSize(14)    def setMinSize(self, minfs):        f = self.font()        f.setPixelSize(minfs)        br = QtGui.QFontMetrics(f).boundingRect(self.text())        self.setMinimumSize(br.width(), br.height())    def resizeEvent(self, event):        super(myQLabel, self).resizeEvent(event)        if not self.text(): return        #--- fetch current parameters ----        f = self.font()        cr = self.contentsRect()        #--- find the font size that fits the contentsRect ---        fs = 1      while True: f.setPixelSize(fs) br =  QtGui.QFontMetrics(f).boundingRect(self.text()) if br.height() <= cr.height() and br.width() <= cr.width():     fs += 1 else:     f.setPixelSize(max(fs - 1, 1)) # backtrack     break        #--- update font size ---        self.setFont(f)class myApplication(QtGui.QWidget):    def __init__(self, parent=None):        super(myApplication, self).__init__(parent)        #---- Prepare a Layout ----        grid = QtGui.QGridLayout()        for i in range(3): grid.addWidget(myQLabel('some text'), i, 0)   grid.setRowStretch(i, i+1) grid.setRowMinimumHeight(i, 25)        self.setLayout(grid)        self.resize(500, 300)if __name__ == '__main__':    app = QtGui.QApplication(sys.argv)    instance = myApplication()      instance.show()    sys.exit(app.exec_())

结果是:

更新-resizeEvent的优化:

下面是该

resizeEvent
方法的优化版本,应该可以产生更好的性能。它大大减少了寻找最佳字体大小值所需的迭代次数。我还没有进行广泛的测试。

def resizeEvent(self, event):    super(myQLabel, self).resizeEvent(event)    if not self.text():        return    #--- fetch current parameters ----    f = self.font()    cr = self.contentsRect()    #--- iterate to find the font size that fits the contentsRect ---    dw = event.size().width() - event.oldSize().width()   # width change    dh = event.size().height() - event.oldSize().height() # height change    fs = max(f.pixelSize(), 1) while True:        f.setPixelSize(fs)        br =  QtGui.QFontMetrics(f).boundingRect(self.text())        if dw >= 0 and dh >= 0: # label is expanding if br.height() <= cr.height() and br.width() <= cr.width():     fs += 1 else:     f.setPixelSize(max(fs - 1, 1)) # backtrack     break        else: # label is shrinking if br.height() > cr.height() or br.width() > cr.width():     fs -= 1 else:     break        if fs < 1: break    #--- update font size ---    self.setFont(f)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517179.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号