0

I have a QTextBrowser and I want to select a part of the text inside, I need the position of the start and the end of the selection. I want to do that with mousePressEvent and mouseReleaseEvent. Here is my code,

class MainWindow(QMainWindow, TeamInsight.Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
    def set_text(self):
        self.textBrowser.setText('test strings are here')

textBrowser is inside a MainWindow. How do I implement mousePressEvent and mouseReleaseEvent for text in textBrowser

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
  • QTextBrowser already implements text selection by mouse. Why do you want to implement it again? – eyllanesc Apr 11 '18 at 07:05
  • I want to trigger another method when selected string positions are equal to specific values. – Ishara Madhawa Apr 11 '18 at 07:10
  • okay, I understand, when you say *I need the position of the start and the end of the selection*, what types of units do you mean, to pixels? – eyllanesc Apr 11 '18 at 07:14
  • I mean the positions of the starting character and the ending character of the selection. For an example: suppose string is 'test' . Postion of the letter 'e' is 1. Position of the 's' is 2. – Ishara Madhawa Apr 11 '18 at 07:19
  • `self.browserInput.selectionChanged.connect(self.position) def position(self): start = self.browserInput.textCursor().selectionStart() end = self.browserInput.textCursor().selectionEnd()` This code gives me a partial solution. But it gives multiple values when I do the selection. – Ishara Madhawa Apr 11 '18 at 07:24
  • suppose I select 'es' in 'test'. It gives values as: 1, 1 and 1, 2 Because that signal is called each time I select a character and not when I press the mouse and the end of the selection. – Ishara Madhawa Apr 11 '18 at 07:28
  • If I understand you, check my answer :D – eyllanesc Apr 11 '18 at 07:34

1 Answers1

2

If you want to track events and you can not overwrite the class, the solution is to install an event filter, in your case, just the MouseButtonRelease event, we must filter the viewport() of the QTextBrowser:

import sys

from PyQt5.QtCore import QEvent
from PyQt5.QtWidgets import QMainWindow, QApplication

import TeamInsight


class MainWindow(QMainWindow, TeamInsight.Ui_MainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.browserInput.viewport().installEventFilter(self)
        self.browserInput.setText("some text")

    def eventFilter(self, obj, event):
        if obj is self.browserInput.viewport():
            if event.type() == QEvent.MouseButtonRelease:
                if self.browserInput.textCursor().hasSelection():
                    start = self.browserInput.textCursor().selectionStart()
                    end = self.browserInput.textCursor().selectionEnd()
                    print(start, end)
            elif event.type() == QEvent.MouseButtonPress:
                print("event mousePressEvent")
        return QMainWindow.eventFilter(self, obj, event)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())
eyllanesc
  • 235,170
  • 19
  • 170
  • 241