Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Avoiding multiple text selections
Qt 6.11 is out! See what's new in the release blog

Avoiding multiple text selections

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 392 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jeanas
    wrote on last edited by
    #1

    Hi!

    I'm working on a PyQt5-based application which contains multiple QPlainTextEdits and QTextBrowsers. I noticed a behavior that I find a bit unintuitive: those widgets can each keep their own text selections independently. For example:

    from PyQt5.QtWidgets import (
        QApplication,
        QMainWindow,
        QWidget,
        QVBoxLayout,
        QPlainTextEdit,
        QTextBrowser,
    )
    
    
    class MainWindow(QMainWindow):
        def __init__(self):
            super(MainWindow, self).__init__()
            layout = QVBoxLayout()
            widget = QWidget()
            widget.setLayout(layout)
            self.setCentralWidget(widget)
    
            editor = QPlainTextEdit(widget)
            layout.addWidget(editor)
            browser = QTextBrowser(widget)
            browser.append("ABCDEF")
            layout.addWidget(browser)
    
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()
    

    If you enter some text in the QPlainTextEdit and select it, then select something in the QTextBrowser, you get two simultaneous selections.

    I found that I can avoid this with a mixin class that does

    def focusOutEvent(self, ev):
        cursor = self.textCursor()
        cursor.clearSelection()
        self.setTextCursor(cursor)
    

    My question: is there a simpler way to do this, rather than changing each of the widget classes? Can I just tell Qt "clear the text selection whenever a widget is unfocused"?

    J 1 Reply Last reply
    0
    • J jeanas

      Hi!

      I'm working on a PyQt5-based application which contains multiple QPlainTextEdits and QTextBrowsers. I noticed a behavior that I find a bit unintuitive: those widgets can each keep their own text selections independently. For example:

      from PyQt5.QtWidgets import (
          QApplication,
          QMainWindow,
          QWidget,
          QVBoxLayout,
          QPlainTextEdit,
          QTextBrowser,
      )
      
      
      class MainWindow(QMainWindow):
          def __init__(self):
              super(MainWindow, self).__init__()
              layout = QVBoxLayout()
              widget = QWidget()
              widget.setLayout(layout)
              self.setCentralWidget(widget)
      
              editor = QPlainTextEdit(widget)
              layout.addWidget(editor)
              browser = QTextBrowser(widget)
              browser.append("ABCDEF")
              layout.addWidget(browser)
      
      app = QApplication([])
      window = MainWindow()
      window.show()
      app.exec()
      

      If you enter some text in the QPlainTextEdit and select it, then select something in the QTextBrowser, you get two simultaneous selections.

      I found that I can avoid this with a mixin class that does

      def focusOutEvent(self, ev):
          cursor = self.textCursor()
          cursor.clearSelection()
          self.setTextCursor(cursor)
      

      My question: is there a simpler way to do this, rather than changing each of the widget classes? Can I just tell Qt "clear the text selection whenever a widget is unfocused"?

      J Offline
      J Offline
      jeanas
      wrote on last edited by
      #2

      And of course, 5 minutes after asking, I found that I can connect my QApplication's focusChanged signal to something like

      def clear_selection(old, new):
          if isinstance(old, (QPlainTextEdit, QTextEdit)):
              cursor = old.textCursor()
              cursor.clearSelection()
              old.setTextCursor(cursor)
      

      I still wonder if there is a better way, though.

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved