防止在QScintilla中触发键盘快捷键(示例代码) [英] Preventing keyboard shortcuts being triggered in QScintilla (example code)

查看:238
本文介绍了防止在QScintilla中触发键盘快捷键(示例代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想防止在QScintilla小部件中编辑代码时触发应用程序键盘快捷键,就像普通的QLineEdit字段不会那样.

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.

在下面的可执行示例代码中,由于已将空格键设置为快捷方式,因此无法在QScintilla小部件中键入空格,但是在QLineEdit中它可以正常工作.

In the executable example code below it is not possible to type whitespaces in the QScintilla widget because the spacebar has been set as a shortcut, but in the QLineEdit it works properly.

我想知道QScintilla是否无法正确获取键盘输入(尽管显然可以,因为可以在其中输入字符了).

I wonder if it could be something to do with the QScintilla not grabbing keyboard input properly (though it obviously does, since it is possible to input characters in it).

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()
        vbox = QVBoxLayout()    
        vbox.addWidget(self.sci)
        vbox.addWidget(QLineEdit())
        vbox.addWidget(self.butt)
        self.setLayout(vbox)
        self.addAction(self.act)

def tjosan():
    print "action !!!"

if __name__ == "__main__":
    app = QApplication(sys.argv)
    widg = MyWidget()
    widg.show()
    sys.exit(app.exec_())

推荐答案

您需要过滤ShortcutOverride事件以获取与QLineEdit相同的行为.这是示例的编辑后的版本,演示了执行此操作的方法:

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_())

这篇关于防止在QScintilla中触发键盘快捷键(示例代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆