from PyQt4.QtGui import *
from PyQt4.QtCore import *
try:
_fromUtf8 = QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
class scrollTextLabel(QLabel):
def __init__(self, parent=None):
super(scrollTextLabel, self).__init__(parent)
self.txt = QString()
self.newX = 10
self.t = QTimer()
self.font = QFont(_fromUtf8('微软雅黑, verdana'), 8)
self.connect(self.t, SIGNAL("timeout()"), self.changeTxtPosition)
def changeTxtPosition(self):
if not self.parent().isVisible():
self.t.stop()
self.newX = 10
return
if self.textRect.width() + self.newX > 0:
self.newX -= 5
else:
self.newX = self.width()
self.update()
def setText(self, s):
self.txt = s
self.newX = 10
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setFont(self.font)
painter.setPen(QColor('transparent'));
self.textRect = painter.drawText(QRect(0, -7, self.width(), 25), Qt.AlignHCenter | Qt.AlignVCenter, self.txt)
if self.textRect.width() > self.width():
painter.setPen(QColor(255, 255, 255, 255))
painter.drawText(QRect(self.newX, -7, self.textRect.width(), 25), Qt.AlignLeft | Qt.AlignVCenter, self.txt)
self.t.start(150)
else:
painter.setPen(QColor(255, 255, 255, 255));
self.textRect = painter.drawText(QRect(0, -7, self.width(), 25), Qt.AlignHCenter | Qt.AlignVCenter, self.txt)
self.t.stop()
当文本长度大于控件宽度,且鼠标悬停时才滚动:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
try:
_fromUtf8 = QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
class nameLabel(QLabel):
def __init__(self, parent=None):
super(nameLabel, self).__init__(parent)
self.txt = QString()
self.newX = 0
self.t = QTimer()
self.font = QFont(_fromUtf8('微软雅黑, verdana'), 8)
self.connect(self.t, SIGNAL("timeout()"), self.changeTxtPosition)
def changeTxtPosition(self):
if not self.parent().isVisible():
self.t.stop()
self.newX = 0
return
if self.textRect.width() + self.newX > 0:
self.newX -= 5
else:
self.newX = self.width()
self.update()
def setText(self, s):
self.txt = s
self.newX = 0
self.update()
def enterEvent(self, event):
self.t.start(150)
def leaveEvent(self, event):
self.t.stop()
self.newX = 0
self.update()
def paintEvent(self, event):
painter = QPainter(self)
painter.setFont(self.font)
painter.setPen(QColor('transparent'));
self.textRect = painter.drawText(QRect(0, -7, self.width(), 25), Qt.AlignHCenter | Qt.AlignVCenter, self.txt)
if self.textRect.width() > self.width():
painter.setPen(QColor(255, 255, 255, 255));
painter.drawText(QRect(self.newX, -7, self.textRect.width(), 25), Qt.AlignLeft | Qt.AlignVCenter, self.txt)
else:
painter.setPen(QColor(255, 255, 255, 255));
self.textRect = painter.drawText(QRect(0, -7, self.width(), 25), Qt.AlignLeft | Qt.AlignVCenter, self.txt)
self.t.stop()