PYQT 自动resize 随着内容自动变化长度

下面的 代码,

可以让 label comboBox 等QWidget自动resize、label comboBox 等QWidget随着内容自动变化长度

label comboBox 等QWidget 自动对齐等,截图如下:


PYQT 自动resize 随着内容自动变化长度_第1张图片


PYQT 自动resize 随着内容自动变化长度_第2张图片

代码如下

# -*- coding: utf-8 -*-


from PyQt4 import QtCore, QtGui
import sys
from mainwin import Ui_MainWindow

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)



from PyQt4.QtCore import Qt
from PyQt4.QtGui import QApplication,\
                        QLabel,\
                        QFontMetrics,\
                        QPainter

class MyLabel(QLabel):
    def paintEvent( self, event ):
        painter = QPainter(self)

        metrics = QFontMetrics(self.font())
        elided  = metrics.elidedText(self.text(), Qt.ElideRight, self.width())

        painter.drawText(self.rect(), self.alignment(), elided)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    mainwin = QtGui.QMainWindow()
    #ui = Ui_MainWindow()
    #ui.setupUi(mainwin)

    #centralwidget = QtGui.QWidget(mainwin)


    grid = QtGui.QGridLayout()

    label = QtGui.QLabel()
    txt = "234234"
    metrics = QFontMetrics(label.font())
    w = metrics.width(txt)
    label.setGeometry(QtCore.QRect(20, 20, w, 80))
    label.setObjectName(_fromUtf8("label_3"))
    label.setText(_translate("MainWindow", txt, None))

    grid.addWidget(label, 0, 0)


    l = ['123' , '3451111111111111', '1111111111', '0000000000000', '123']
    comboBox = QtGui.QComboBox()
    txt = (max(l, key=len))
    metrics = QFontMetrics(comboBox.font())
    w = metrics.width(txt)
    comboBox.setGeometry(QtCore.QRect(80, 5, 20, 20))
    comboBox.setMinimumWidth(w)

    comboBox.setObjectName(_fromUtf8("comboBox_2"))
    comboBox.addItems(l)
    comboBox.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents)

    grid.addWidget(comboBox, 0, 1)


    label1 = QtGui.QLabel()
    txt = "2342341111111111111111"
    metrics = QFontMetrics(label1.font())
    w = metrics.width(txt)
    label1.setGeometry(QtCore.QRect(20, 20, w, 80))
    label1.setObjectName(_fromUtf8("label_3"))
    label1.setText(_translate("MainWindow", txt, None))

    grid.addWidget(label1, 1, 0)


    l = ['123' , '3451', '111', '000', '123']
    comboBox1 = QtGui.QComboBox()
    txt = (max(l, key=len))
    metrics = QFontMetrics(comboBox1.font())
    w = metrics.width(txt)
    comboBox1.setGeometry(QtCore.QRect(80, 5, 20, 20))
    comboBox1.setMinimumWidth(w)

    comboBox1.setObjectName(_fromUtf8("comboBox_2"))
    comboBox1.addItems(l)
    comboBox1.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents)

    grid.addWidget(comboBox1, 1, 1)

    l = ['123', '3451', '111', '000', '123']
    comboBox2 = QtGui.QComboBox()
    txt = (max(l, key=len))
    metrics = QFontMetrics(comboBox2.font())
    w = metrics.width(txt)
    comboBox2.setGeometry(QtCore.QRect(80, 5, 20, 20))
    comboBox2.setMinimumWidth(w)

    comboBox2.setObjectName(_fromUtf8("comboBox_2"))
    comboBox2.addItems(l)
    comboBox2.setSizeAdjustPolicy(QtGui.QComboBox.AdjustToContents)
    grid.addWidget(comboBox2, 2, 1)

    #centralwidget = QtGui.QWidget()
    #mainwin.setCentralWidget(centralwidget)

    #tabWidget = QtGui.QTabWidget(centralwidget)
    tabWidget = QtGui.QTabWidget()
    tabWidget.setGeometry(QtCore.QRect(20, 20, 500, 500))
    tabWidget.setObjectName(_fromUtf8("tabWidget"))
    tab = QtGui.QWidget()
    tab.setObjectName(_fromUtf8("tab"))
    tabWidget.addTab(tab, _fromUtf8(""))
    tab_2 = QtGui.QWidget()
    tab_2.setObjectName(_fromUtf8("tab_2"))
    tabWidget.addTab(tab_2, _fromUtf8(""))

    tabWidget.setTabText(tabWidget.indexOf(tab), _translate("MainWindow", "Tab 1", None))
    tabWidget.setTabText(tabWidget.indexOf(tab_2), _translate("MainWindow", "Tab 2", None))

    tabWidget.setDocumentMode(True)

    #tabcentralwidget = QtGui.QWidget(centralwidget)
    #tabWidget.setCentralWidget(tabcentralwidget)
    #tabcentralwidget.setLayout(grid)

    tab.setLayout(grid)

    mainwin.setCentralWidget(tabWidget)

    mainwin.show()
    app.exec_()



你可能感兴趣的:(其他技巧)