1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget,
QVBoxLayout, QTextEdit, QTextBrowser)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QTextCursor
import sys
class text_browser(QTextBrowser):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setOpenExternalLinks(True)
self.setMouseTracking(True)
self.setReadOnly(True)
self.__nblig=0
def append(self, s):
self.__nblig+=1
super().append(s)
def mouseMoveEvent(self, event):
position = event.pos()
cursor = self.cursorForPosition(position)
cursor_word = self.cursorForPosition(position)
cursor.select(QTextCursor.BlockUnderCursor)
cursor_word.select(QTextCursor.WordUnderCursor)
self.line = cursor.selectedText()
self.word = cursor_word.selectedText()
print("line=[%s], nb=%d, word_survolé=[%s]" % (self.line, self.__nblig, self.word), cursor.atEnd())
super().mouseMoveEvent(event)
def mousePressEvent(self, event):
position = event.pos()
cursor = self.cursorForPosition(position)
cursor.select(QTextCursor.BlockUnderCursor)
if self.word == "X":
cursor.select(QTextCursor.LineUnderCursor)
cursor.removeSelectedText()
cursor.deleteChar()
super().mousePressEvent(event)
class Fenetre_Principale(QMainWindow):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
link_format = ('<a href="C:/Users/clement/Desktop/tuto3.pdf">{}</a>' +
'<span style="color:black;">{}</span>' +
'<a href="#" role="button">{}</a>')
self.setCentralWidget(QWidget(parent=self))
edit = text_browser(parent=self.centralWidget())
edit.append(link_format.format("ligne1", " " ,"X"))
edit.append(link_format.format("ligne2", " " ,"X"))
edit.append(link_format.format("ligne3", " " ,"X"))
edit.append(link_format.format("ligne4", " " ,"X"))
layout_vertical_global=QVBoxLayout(self.centralWidget())
layout_vertical_global.addWidget(edit)
self.setFixedSize(600, 200)
self.show()
if __name__ == "__main__":
appli = QApplication(sys.argv)
fenetre_main = Fenetre_Principale()
sys.exit(appli.exec()) |
Partager