Preventing keyboard shortcuts being triggered in QScintilla (example code)

前端 未结 1 1964
长情又很酷
长情又很酷 2021-01-22 00:43

I would like to prevent application keyboard shortcuts from being triggered when editing code in my QScintilla widget, just like a normal QLineEdit field doesn\'t.

In t

相关标签:
1条回答
  • 2021-01-22 01:22

    You need to filter ShortcutOverride events to get the same behaviour as QLineEdit. Here's an edited version of your example that demonstrates a way to do that:

    import sys,os
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    from PyQt4 import Qsci
    
    class MyWidget(QWidget):
        def __init__(self):
            QWidget.__init__(self)
            self.butt = QPushButton("button!!")
            self.act = QAction("new act",self)
            self.act.setShortcut(QKeySequence(Qt.Key_Space))
            self.act.triggered.connect(tjosan)
            self.butt.clicked.connect(self.act.trigger)
            self.sci = Qsci.QsciScintilla()
            self.sci.installEventFilter(self)
            vbox = QVBoxLayout()
            vbox.addWidget(self.sci)
            vbox.addWidget(QLineEdit())
            vbox.addWidget(self.butt)
            self.setLayout(vbox)
            self.addAction(self.act)
    
        def eventFilter(self, widget, event):
            if (event.type() == QEvent.ShortcutOverride and
                widget is self.sci):
                print 'ShortcutOverride'
                event.accept()
                return True
            return QWidget.eventFilter(self, widget, event)
    
    def tjosan():
        print "action !!!"
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        widg = MyWidget()
        widg.show()
        sys.exit(app.exec_())
    
    0 讨论(0)
提交回复
热议问题