Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QShortcuts catch external shortcuts from ReadOnly textedit
Forum Update on Monday, May 27th 2025

QShortcuts catch external shortcuts from ReadOnly textedit

Scheduled Pinned Locked Moved Unsolved Qt for Python
5 Posts 2 Posters 413 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.
  • N Offline
    N Offline
    nutrx
    wrote on last edited by
    #1

    Just a quick question: I am declaring manual shortcuts in a widget

            copy_shortcut = QShortcut(QKeySequence.Copy, self)
            copy_shortcut.activated.connect(self.copy)
    

    and I noticed that this also catches those performed when I'm focused on a text edit which is ReadOnly. Which isn't what I want, I want the focused widget to process the event. It works when the text edit is editable, but doesn't when its readonly. I also tried calling

            textedit.setFocusPolicy(Qt.ClickFocus)
    

    which didn't help. How can I do this?
    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you provide a full minimal script that shows that behaviour ?
      From your snippet it's not clear how you are setting everything up.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Can you provide a full minimal script that shows that behaviour ?
        From your snippet it's not clear how you are setting everything up.

        N Offline
        N Offline
        nutrx
        wrote on last edited by nutrx
        #3
        from PySide2.QtWidgets import QMainWindow, QApplication, QWidget, QVBoxLayout, QPlainTextEdit, QShortcut, QPushButton
        from PySide2.QtGui import QKeySequence, QGuiApplication
        import sys
        
        
        class CatchingWidget(QWidget):
            def __init__(self, parent):
                super().__init__(parent)
        
                copy_shortcut = QShortcut(QKeySequence.Copy, self)
                copy_shortcut.activated.connect(self.copy)
        
                self.setLayout(QVBoxLayout())
                self.layout().addWidget(QPushButton())
        
            def copy(self):
                print('copy in widget!')
                QGuiApplication.clipboard().setText(self.hasFocus())
        
        
        class MainWindow(QMainWindow):
            def __init__(self):
                super().__init__()
        
                l = QVBoxLayout()
                l.addWidget(CatchingWidget(self))
                textedit = QPlainTextEdit('some text I would like to copy', self)
                textedit.setReadOnly(True)
                l.addWidget(textedit)
        
                w = QWidget()
                w.setLayout(l)
                self.setCentralWidget(w)
        
        
        if __name__ == '__main__':
            app = QApplication()
            mw = MainWindow()
            mw.show()
            sys.exit(app.exec_())
        

        This has a widget at the top, the textedit below. If you try to copy text from it using ctrl+c, still the upper widget's copy method is triggered instead, even if it doesn't have focus.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nutrx
          wrote on last edited by nutrx
          #4

          this is still really annoying, is this a bug or am I just missing sth? I didn't find anything regarding this in the docs so far. Also found this old bugreport

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            What if you set the shortcut context to Qt.WidgetWithChildrenShortcut ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

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