I am writing a Help/About box that reads the README.md and displays it. If about is passed in, it is supposed to scroll to that portion of the file and put the about at the top. This code correctly locates the text in the file and moves the cursor, but the located text is at the bottom of the browser (and remains selected - I would like to-deselect it). I have also tried just searching for the text:
text_browser.find(section)
but the found text always remains at the bottom of the browser.
class HelpWindow(QDialog):
def __init__(self, readme_file, about=None):
super().__init__()
if not os.path.isfile(readme_file):
raise RuntimeError(f'{readme_file} is not a valid filename')
top_layout = QVBoxLayout()
text_browser = QTextBrowser(minimumWidth=800, minimumHeight=600)
text_browser.setSource(QUrl(readme_file))
if about:
cursor = text_browser.textCursor()
cursor.movePosition(QTextCursor.MoveOperation.End)
text_browser.setTextCursor(cursor)
text_browser.find(about, QTextDocument.FindFlag.FindBackward)
text_browser.textCursor().clearSelection()
top_layout.addWidget(text_browser)
layout = QHBoxLayout()
button = QPushButton('Cancel')
button.setSizePolicy(QSizePolicy.Policy.Maximum, QSizePolicy.Policy.Maximum)
# noinspection PyUnresolvedReferences
button.clicked.connect(self.cancel)
layout.addWidget(button)
top_layout.addLayout(layout)
self.setLayout(top_layout)
Also,
text_browser.textCursor().clearSelection()
does not work here for some reason.
So, this DOES work, but I don't like it:
text_browser.find(section)
pyautogui.press('pagedown')
pyautogui.press('up')
pyautogui.press('up')
I found a much better solution using tags:
In the README.md file, include a tag where you want to jump to:
### Format <a name="jump-here">.</a>
*** NOTE this unfortunately leaves a blue dot at the end of the line. I have tried:
### Format <a name="jump-here"></a>
### Format <a name="jump-here"/>
but neither one work :-(
In the code, open the file with the tag at the end of the file name:
f = 'README.md#jump-here'
text_browser.setSource(QUrl(f))
text_browser.scrollToAnchor(section)