Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

How to turn lines of text in PyQt6 QTextEdit into clickable links that call a function?

+0
−1

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 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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

Relevant information of the "experimenting and researching" you have done (1 comment)

0 answers

Sign up to answer this question »