带有HTML链接的QTextDocument问题

2024-04-30 01:43:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用PyQt5构建一个简单的选框小部件来显示一些应该从左向右滚动的新闻。新闻文本应该包含链接,因此用户可以单击它们并阅读完整的新闻帖子(在网站上)。我使用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什么也没有?有什么提示吗?在

screenshot


Tags: selfnoneeventifdeflisttimerpx
1条回答
网友
1楼 · 发布于 2024-04-30 01:43:23

因为您使用的是QLabel,所以这应该是微不足道的。只需使用合适的Qt::TextInteractionFlags。在您的例子中是Qt::LinksAccessibleByMouse。在

文档清楚地告诉您:

enum  Qt::TextInteractionFlags
flags Qt::TextInteractionFlags

...
Qt::LinksAccessibleByMouse      4   Links can be highlighted and activated with the mouse.
Qt::LinksAccessibleByKeyboard   8   Links can be focused using tab and activated with enter.
...

相关问题 更多 >