我正在使用 PyQt5 构建一个简单的 Marquee 小部件来显示一些应该从左到右滚动的新闻。新闻文本应该包含链接,因此用户可以单击它们并阅读完整的新闻帖子(在网站上)。我正在使用 QTextDocument 在 HTML 中呈现新闻标题(连同 href url):虽然 HTML 呈现完美,但我无法单击生成的 href 标记。下面是我正在使用的代码:
class MarqueeLabel(QLabel):
px = 0
py = 0
paused = False
qthread = None
refresh_msec = 30
textDocument = None
textWidth = 0
timer = None
def __init__(self, parent=None):
QLabel.__init__(self, parent)
self.initUI()
def initUI(self):
self.setFixedSize(433, 40)
self.setStyleSheet("""
QLabel {
background: transparent;
color: #484848;
}
""")
def parseFeed(self):
class FeedParser(QThread):
msignal = pyqtSignal(list, list)
def run(self):
try:
feed = feedparser.parse(URL)
except Exception, e:
pass
else:
feedhtml = []
feedtext = []
for item in feed['entries']:
feedhtml.append('%(TEXT)s (<a href="%(LINK)s">read more</a>)' % dict(LINK=item['link'], TEXT=item['title']))
feedtext.append('%s (read more)' % item['title'])
self.msignal.emit(feedhtml, feedtext)
self.qthread = FeedParser(self)
self.qthread.msignal.connect(self.setMarqueeText)
self.qthread.finished.connect(self.start)
self.qthread.start()
@pyqtSlot(list, list)
def setMarqueeText(self, feedhtml, feedtext):
## FIXME: the 1.1 multiplier should not be here, but without the text goes on 2 lines!
plaintext = ' - '.join(feedtext)
self.textWidth = self.fontMetrics().boundingRect(plaintext).width() * 1.1
self.textDocument = QTextDocument(self)
self.textDocument.setUndoRedoEnabled(False)
self.textDocument.setUseDesignMetrics(True)
self.textDocument.setTextWidth(self.textWidth)
self.textDocument.setHtml(' - '.join(feedhtml))
self.px = self.width()
self.py = self.height() - self.fontMetrics().boundingRect(plaintext).height() - 6
def start(self):
if self.textDocument:
self.timer = QTimer(self)
self.timer.timeout.connect(self.scrollText)
self.timer.start(self.refresh_msec)
def event(self, event):
if event.type() == QEvent.Enter:
self.paused = True
elif event.type() == QEvent.Leave:
self.paused = False
return QLabel.event(self, event)
def paintEvent(self, QPaintEvent):
if self.textDocument:
p = QPainter(self)
p.translate(self.px, self.py)
self.textDocument.drawContents(p, QRectF(self.rect()))
def scrollText(self):
if not self.paused:
if -self.px > self.textWidth:
self.px = self.width()
else:
self.px -= 1
self.repaint()
这是一个显示正确呈现的 HTML 的屏幕截图,尽管点击 URL 什么也没做?有什么提示吗?