Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
A while back, I made this post about reading specified sections in a text file formatted a particular way. I've since updated the format for that file to include links at the start of arbitrary li...
#2: Post edited
How to turn lines of text in PyQt6 QTextEdit into clickable links that call a function
- How to turn lines of text in PyQt6 QTextEdit into clickable links that call a function?
#1: Initial revision
How to turn lines of text in PyQt6 QTextEdit into clickable links that call a function
A while back, I made [this post](https://software.codidact.com/posts/293660) about reading specified sections in a text file formatted a particular way. I've since updated the format for that file to include links at the start of arbitrary lines within the section text, formatted as `{sectionNumber some text}`. The problem is, I now need to make those links actually work. The idea is that when someone clicks on a link, it calls the readSection() function with the section number specified in the link passed as a parameter. For example: the line formatted as `{2 Go to section 2}` would show up as `Go to section 2` (already handled; see below) and when it's clicked, it would invoke tthe function `readSection(2)`. Here's the code I have so far: ``` def readSection(sectionNumber): global myFile with open(myFile) as file: # Open the file firstLineNumber = None # Create a variable for the first line number of the section sectionLines = [] # Create a variable for listing all the lines in the section for lineNumber, line in enumerate(file, start=1): # Iterate over every line and line number in the file if line.startswith('[' + str(sectionNumber)): # If the line starts with a [ and the section number... firstLineNumber = lineNumber # Mark the first line number as found sectionLines.append(line) # Add the line to the list elif line[0] == ']' and firstLineNumber: # If the line starts with a ] and the first line number is marked as found... sectionLines.append(line) # Add the line to the list break # Break the for loop, since first and last line numbers have been found elif firstLineNumber is not None: # If the first line has been added and the last one hasn't... if line.startswith('{'): # If the line is a link... line = line[3:] # Slice the first three characters from it (the opening bracket, page number, and extra space) line = line.replace('}', '') # Remove the closing bracket sectionLines.append(line) # Add the line to the list sectionLines.pop(0) # Remove the line starting with [ and the section number from the section lines sectionLines.pop(-1) # Remove the line starting with ] from the section lines newSection = "".join(sectionLines) # Turn the list of lines into a prettier looking section window.buffer.setText(newSection) # Insert it into the QTextEdit ``` I was thinking I could use toMarkdown() or toHtml() on the QTextEdit, replace the links with those respective formats, and then intercept the link activation events somehow, but I've done a lot of experimenting and researching and can't get this to work.