一半君的总结纸

听话只听一半君

带”overflow”效果的 QLabel 和 QPushButton

有时候Label上的text太长,但是当窗口足够小的时候,想让text自动缩略成 xxxx …

搜到的
http://stackoverflow.com/a/11764662/2052889
elide_label

人肉备份

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 = None
    if ( not QApplication.instance() ):
        app = QApplication([])

    label = MyLabel()
    label.setText('This is a really, long and poorly formatted runon sentence used to illustrate a point')
    label.setWindowFlags(Qt.Dialog)
    label.show()

    if ( app ):
        app.exec_()

同样原理,我想要一个Button上显示的长string也自动缩略

elide_button

# coding: utf8
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QApplication,\
    QLabel,\
    QFontMetrics,\
    QPainter

from PyQt4 import QtGui, QtCore

class ElideButton(QtGui.QPushButton):

    def __init__(self, parent=None):

        super(ElideButton, self).__init__(parent)
        font = self.font()
        font.setPointSize(9)
        self.setFont(font)

    def paintEvent(self, event):
        painter = QtGui.QStylePainter(self)

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

        option = QtGui.QStyleOptionButton()
        self.initStyleOption(option)
        option.text = ''
        painter.drawControl(QtGui.QStyle.CE_PushButton, option)
        painter.drawText(self.rect(), Qt.AlignCenter, elided)

        # 或者改成
        # option.text = elided 
        # painter.drawControl(QtGui.QStyle.CE_PushButton, option)

if (__name__ == '__main__'):
    app = None
    if (not QApplication.instance()):
        app = QApplication([])

    button = ElideButton()
    button.setText(
        u'This is a really, long and poorly formatted runon sentence used to illustrate a point')
    button.setWindowFlags(Qt.Dialog)
    button.show()

    app.exec_()

但是这里有个问题,drawText显示的text太靠左了(比默认的button上的字要更靠左),我后来用了个脑残法,但是感觉肯定不对… 虽然效果有了…请会的人教下小弟…

改成

painter.drawText(self.rect(), Qt.AlignCenter, ' '+ elided)

# 或者
elided = metrics.elidedText(self.text(), Qt.ElideRight, self.width()-10)
...
x,y,w,h = self.rect().getRect()
newRect = QtCore.QRect(x+10,y,w,h)
painter.drawText(newRect, Qt.AlignCenter, elided)

上图中不明显,请看此图
elide_button_bug

我在上图中用的是

painter.drawText(self.rect(),Qt.AlignLeft| Qt.AlignVCenter,elided)
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: