Python 如何将QTextBrowser(其中包含html表)的光标移动到PyQt5中的特定行?
我创建了一个QTextBrowser来在代码中显示一个html表。但是,当我尝试使用setTextCursor方法将光标移动到特定行时,它失败了 文本浏览器的滚动条确实移动了,但没有移动到特定的行。这个问题与html表有关吗Python 如何将QTextBrowser(其中包含html表)的光标移动到PyQt5中的特定行?,python,qt,pyqt,Python,Qt,Pyqt,我创建了一个QTextBrowser来在代码中显示一个html表。但是,当我尝试使用setTextCursor方法将光标移动到特定行时,它失败了 文本浏览器的滚动条确实移动了,但没有移动到特定的行。这个问题与html表有关吗 import sys from PyQt5.QtGui import QTextCursor from PyQt5.QtWidgets import QWidget, QTextBrowser, QMainWindow, QPushButton, QHBoxLayout,
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_())
导入系统
从PyQt5.QtGui导入QTextCursor
从PyQt5.QtWidgets导入QWidget、QTextBrowser、QMainWindow、QPushButton、QHBoxLayout、QApplication
类MyTextBrowser(QTextBrowser):
def uuu init uuu(self,parent=None):
超级(MyTextBrowser,self)。\uuuu init\uuuuu(父级)
self.createTable()
def createTable(自身,行数=1):
#创建一个包含100行的html表
html=“”
对于范围(0,100)内的i:
#突出显示指定行
如果行_num==i+1:
html+='行%d'(i+1)
其他:
html+='行%d'(i+1)
html+=''
self.setHtml(html)
#将光标移动到指定行
cursor=QTextCursor(self.document().findBlockByLineNumber(line_num))
self.setTextCursor(游标)
类MyWindow(QMainWindow):
def uuu init uuu(self,parent=None):
超级(MyWindow,self)。\uuuuu init\uuuuuu(父级)
self.createLayout()
def createLayout(自):
#创建文本浏览器和按钮
self.textBrowser=MyTextBrowser()
self.button=QPushButton(“移动光标”)
self.button.clicked.connect(self.buttonClicked)
self.currentLine=1
布局=QHBoxLayout()
layout.addWidget(self.button)
layout.addWidget(self.textBrowser)
window=QWidget()
window.setLayout(布局)
self.setCentralWidget(窗口)
def按钮已锁定(自):
#单击按钮时,将光标向下移动10行
自身电流线+=10
如果self.currentLine>100:
self.currentLine=1
self.textBrowser.createTable(self.currentLine)
app=QApplication(sys.argv)
window=MyWindow()
窗口。调整大小(640480)
window.show()
sys.exit(app.exec_())
经过一番尝试后,我发现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标记数