如何将 QTextBrowser(其中包含 html 表)的光标移动到 PyQt5 中的特定行?

     2023-02-17     179

关键词:

【中文标题】如何将 QTextBrowser(其中包含 html 表)的光标移动到 PyQt5 中的特定行?【英文标题】:How to move the cursor of a QTextBrowser(with an html table in it) to a specific line in PyQt5? 【发布时间】:2017-05-23 11:28:15 【问题描述】:

我创建了一个 QTextBrowser 来在我的代码中显示一个 html 表格。但是当我尝试使用 setTextCursor 方法将光标移动到特定行时,它没有做到这一点。

文本浏览器的滚动条确实移动了,但没有移动到特定的行。这个问题是不是和html表有关?

import sys
from PyQt5.QtGui import QTextCursor
from PyQt5.QtWidgets import QWidget, QTextBrowser, QMainWindow, QPushButton, QHBoxLayout, QApplication

class MyTextBrowser(QTextBrowser):

    def __init__(self, parent = None):
        super(MyTextBrowser, self).__init__(parent)
        self.createTable()

    def createTable(self, line_num = 1):
        # Create an html table with 100 lines
        html = '<table><tbody>'
        for i in range(0, 100):
            # Highlight specified line
            if line_num == i+1:
                html += '<tr style="background-color: #0000FF;"><td>Line</td><td>%d</td></tr>' % (i+1)
            else:
                html += '<tr><td>Line</td><td>%d</td></tr>' % (i+1)
        html += '</tbody></table>'
        self.setHtml(html)

        # Move the cursor to the specified line
        cursor = QTextCursor(self.document().findBlockByLineNumber(line_num))
        self.setTextCursor(cursor)

class MyWindow(QMainWindow):

    def __init__(self, parent = None):
        super(MyWindow, self).__init__(parent)
        self.createLayout()

    def createLayout(self):
        # Create the text browser and a button
        self.textBrowser = MyTextBrowser()
        self.button = QPushButton('Move cursor')
        self.button.clicked.connect(self.buttonClicked)
        self.currentLine = 1

        layout = QHBoxLayout()
        layout.addWidget(self.button)
        layout.addWidget(self.textBrowser)

        window = QWidget()
        window.setLayout(layout)
        self.setCentralWidget(window)

    def buttonClicked(self):
        # Move the cursor down for 10 lines when the button is clicked
        self.currentLine += 10
        if self.currentLine > 100:
            self.currentLine = 1

        self.textBrowser.createTable(self.currentLine)

app = QApplication(sys.argv)
window = MyWindow()
window.resize(640, 480)
window.show()
sys.exit(app.exec_())

【问题讨论】:

【参考方案1】:

经过一番尝试,我发现 QTextBrowser 似乎将每个 td 标记视为一个新行。

所以,而不是使用

cursor = QTextCursor(self.document().findBlockByLineNumber(line_num))
self.setTextCursor(cursor)

我们应该使用

cursor = QTextCursor(self.document().findBlockByLineNumber(line_num * td_num))
self.setTextCursor(cursor)

其中td_num是表格每一行中td标签的数量。

【讨论】:

单击其中的链接后,QTextBrowser 中的 PySide/PyQt 文本消失

】单击其中的链接后,QTextBrowser中的PySide/PyQt文本消失【英文标题】:PySide/PyQtTextinQTextBrowserdisappearsafterclickingonalinkinit【发布时间】:2013-10-1817:09:52【问题描述】:我将以下变量附加到“QTextBrowser”。它确实显示为链接,但是当... 查看详情

使用包含 QProcess 枚举的 QString 在 QTextBrowser 中设置文本

】使用包含QProcess枚举的QString在QTextBrowser中设置文本【英文标题】:SettextinQTextBrowserwithaQStringthatincludesQProcessenums【发布时间】:2012-04-0518:26:17【问题描述】:我想将包含两个QProcess枚举的QString写入QTextBrowser。因此我使用了我的... 查看详情

如何在 QTextBrowser 中居中选定文本

】如何在QTextBrowser中居中选定文本【英文标题】:HowtotocenterselectedtextinQTextBrowser【发布时间】:2017-01-0701:30:44【问题描述】:下面发布的代码创建了QTextBrowser窗口,其中填充了100行文本:从MESSAGE-0000一直到MESSAGE-0099fromPyQt4importQtC... 查看详情

如何选择 QTextBrowser 中的所有匹配项

】如何选择QTextBrowser中的所有匹配项【英文标题】:HowtoselectalltheoccurrencesinQTextBrowser【发布时间】:2017-01-1919:56:43【问题描述】:代码创建了QTextBrowser窗口,其中填充了文本行。我想选择所有匹配“长线”的词如何实现?fromPyQt4... 查看详情

如何将 QTextBrowser 的多个实例打印到一个 PDF 文件中?

】如何将QTextBrowser的多个实例打印到一个PDF文件中?【英文标题】:HowtoprintmultipleinstancesofQTextBrowserintoonePDFfile?【发布时间】:2015-05-2613:56:51【问题描述】:我正在开发的QT应用程序附带一个教程。每章都是一个独立的HTML文件,... 查看详情

在 QTextBrowser 中以不同方式处理不同的链接

】在QTextBrowser中以不同方式处理不同的链接【英文标题】:HandlingdifferentlinksdifferentlyinQTextBrowser【发布时间】:2017-05-3002:49:59【问题描述】:我正在尝试设置一个QTextBrowser,其中大多数超链接正常工作,但以任意前缀开头的超链... 查看详情

如何控制QTextBrowser水平滚动条

】如何控制QTextBrowser水平滚动条【英文标题】:HowtocontrolQTextBrowserhorizontalscrollbar【发布时间】:2017-01-1917:24:56【问题描述】:下面的代码创建了一个QTextBrowser小部件,用长文本行填充它。由于应用了view.setLineWrapMode(0),因此视图... 查看详情

如何让 QTextBrowser 显示 QTextEdit 的内容?

】如何让QTextBrowser显示QTextEdit的内容?【英文标题】:HowtohaveaQTextBrowsertodisplaycontentsofaQTextEdit?【发布时间】:2014-03-3120:39:48【问题描述】:我正在尝试将QTextEdit连接到QTextBrowser,因此文本浏览器小部件会输出在文本编辑小部件... 查看详情

Qt - 如何使 QTextBrowser 调整为内容(垂直布局)

】Qt-如何使QTextBrowser调整为内容(垂直布局)【英文标题】:Qt-HowtomakeQTextBrowserresizetocontents(verticallayout)【发布时间】:2014-08-2815:25:31【问题描述】:我的对话框中有一个QTextBrowser对象,该对象将写入HTML。它与其他几个对象处于... 查看详情

Python - pyqt5 - 使用特定字体时 qtextbrowser 中的文本对齐出错

】Python-pyqt5-使用特定字体时qtextbrowser中的文本对齐出错【英文标题】:Python-pyqt5-Alignmentoftextgoeswronginqtextbrowserwhenworkingwithaspecificfont【发布时间】:2018-04-1705:29:24【问题描述】:我有一个字符串数组,其中包含特定字体的字符串(... 查看详情

PyQt如何添加到QTextBrowser的链接

】PyQt如何添加到QTextBrowser的链接【英文标题】:PyQthowtoaddlinktoQTextBrowser【发布时间】:2012-04-1301:22:27【问题描述】:我不知道为什么我很难找到这个,但是如何添加指向QTextEdit或QTextBrowser的超链接?我正在尝试制作具有链接列... 查看详情

QTextBrowser / C++ 附加可点击文本

】QTextBrowser/C++附加可点击文本【英文标题】:QTextBrowser/C++appendclickabletext【发布时间】:2017-05-1609:16:52【问题描述】:我是使用QT的新手,但遇到了问题。我想检查2D电路布局,并且想在窗口上输出警告或错误。每个警告或错误... 查看详情

如何清除pyqt5中qtextbrowser的内容?

】如何清除pyqt5中qtextbrowser的内容?【英文标题】:howdoIclearthecontentsofqtextbrowserinpyqt5?【发布时间】:2020-03-2315:34:09【问题描述】:如何清除QTextBrowser的内容?我试过clearHistory()以及设置光标选项,但没有帮助有谁知道如何清除Qte... 查看详情

如何将 jar 部署到 glassfish,其中包含一个主要方法?

】如何将jar部署到glassfish,其中包含一个主要方法?【英文标题】:Howtodeployajartoglassfish,whichcontainsamainmethod?【发布时间】:2015-11-1011:00:41【问题描述】:我是EJB的新手。我有一个包含“Class1”的ejb-jar文件,我将它部署到glassfish... 查看详情

如何删除 QTextBrowser 中的虚线轮廓链接?

】如何删除QTextBrowser中的虚线轮廓链接?【英文标题】:HowtoremovedottedoutlinealinkinQTextBrowser?【发布时间】:2016-10-3102:49:30【问题描述】:我用QTextCharFormat格式化链接样式并插入QTextBrowser,当点击这个链接时,它显示虚线轮廓(我... 查看详情

qtextbrowser 右对齐或居中

】qtextbrowser右对齐或居中【英文标题】:qtextbrowseralignrightorcenter【发布时间】:2012-08-2020:44:19【问题描述】:我试图为我的应用程序创建某种帮助系统。QTextBrowser和一个静态HTML文件对我来说似乎很完美。唯一的问题是QTextBrowser... 查看详情

如何将数据帧拆分为多个数据帧,其中每个数据帧包含相等但随机的数据[重复]

】如何将数据帧拆分为多个数据帧,其中每个数据帧包含相等但随机的数据[重复]【英文标题】:HowdoIsplitadataframeintomultipledataframeswhereeachdataframecontainsequalbutrandomdata[duplicate]【发布时间】:2017-10-1707:41:03【问题描述】:如何将一... 查看详情

QTextBrowser:如何突出显示单击的行

】QTextBrowser:如何突出显示单击的行【英文标题】:QTextBrowser:Howtohighlightaclickedline【发布时间】:2014-03-2720:05:51【问题描述】:在我的文本浏览器中,我已经实现了mousePress并在单击时找到了行号。现在我想突出显示我单击的位... 查看详情