如何在pyqt(QGIS)中捕捉QDockWidget的KeyPressedEvent
How to catch KeyPressedEvent of QDockWidget in pyqt (QGIS)
我正在尝试在 QDockWidget 的 KeyPressedEvent 中添加一些逻辑。它在没有 QDockWidget 的情况下工作,即在主要 window 上,但在 QDockWidget 上不起作用。
这是我尝试过的:
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from qgis.core import QgsProject
from .progress_bar import Ui_MainWindow_Progress
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QApplication.translate(context, text, disambig)
class Ui_MainWindow(QMainWindow):
def __init__(self, iface):
super().__init__()
self.iface = iface
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(356, 750)
self.pushButton_exit = QPushButton(MainWindow)
self.pushButton_exit.setGeometry(QtCore.QRect(290, 0, 50, 23))
self.pushButton_exit.setObjectName(_fromUtf8("pushButton_exit"))
self.pushButton_exit.setStyleSheet("background-color: red")
self.label_4 = QLabel(MainWindow)
self.label_4.setGeometry(QtCore.QRect(20, 300, 81, 20))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
self.dock_widget = QDockWidget("My Plugin", self.iface.mainWindow())
self.dock_widget.setWidget(MainWindow)
self.dock_widget.setFixedWidth(356)
self.dock_widget.setFixedHeight(750)
self.iface.addDockWidget(QtCore.Qt.RightDockWidgetArea,
self.dock_widget)
self.dock_widget.setFeatures(QDockWidget.DockWidgetFloatable |
QDockWidget.DockWidgetMovable)
self.dock_widget.keyPressEvent = self.keyPressed
def keyPressed(self, event):
if event.key() == QtCore.Qt.Key_Escape:
print("Escape pressed")
代码运行良好,但未调用 keyPressed 方法。
QDockWidget的KeyPressedEvent如何处理?
keyPressEvent()
: is a predefined function in PyQt framework
used to define the functionalities of the key press event generated.
Note that, you can’t change its function name, you can define its
argument content and function body as per your requirement.
event.key() == QtCore.Qt.Key_Escape
is being used under the
keyPresssEvent() function. Here, key_Escape is used to mention that
the key we are going to generate event is the Escape key.
尝试:
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
print("Escape pressed")
我添加了这一行 self.dock_widget.setFocusPolicy(QtCore.Qt.StrongFocus),现在可以正常工作了。
我正在尝试在 QDockWidget 的 KeyPressedEvent 中添加一些逻辑。它在没有 QDockWidget 的情况下工作,即在主要 window 上,但在 QDockWidget 上不起作用。
这是我尝试过的:
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from qgis.core import QgsProject
from .progress_bar import Ui_MainWindow_Progress
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QApplication.translate(context, text, disambig)
class Ui_MainWindow(QMainWindow):
def __init__(self, iface):
super().__init__()
self.iface = iface
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(356, 750)
self.pushButton_exit = QPushButton(MainWindow)
self.pushButton_exit.setGeometry(QtCore.QRect(290, 0, 50, 23))
self.pushButton_exit.setObjectName(_fromUtf8("pushButton_exit"))
self.pushButton_exit.setStyleSheet("background-color: red")
self.label_4 = QLabel(MainWindow)
self.label_4.setGeometry(QtCore.QRect(20, 300, 81, 20))
self.label_4.setObjectName(_fromUtf8("label_4"))
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
self.dock_widget = QDockWidget("My Plugin", self.iface.mainWindow())
self.dock_widget.setWidget(MainWindow)
self.dock_widget.setFixedWidth(356)
self.dock_widget.setFixedHeight(750)
self.iface.addDockWidget(QtCore.Qt.RightDockWidgetArea,
self.dock_widget)
self.dock_widget.setFeatures(QDockWidget.DockWidgetFloatable |
QDockWidget.DockWidgetMovable)
self.dock_widget.keyPressEvent = self.keyPressed
def keyPressed(self, event):
if event.key() == QtCore.Qt.Key_Escape:
print("Escape pressed")
代码运行良好,但未调用 keyPressed 方法。 QDockWidget的KeyPressedEvent如何处理?
keyPressEvent()
: is a predefined function in PyQt framework used to define the functionalities of the key press event generated. Note that, you can’t change its function name, you can define its argument content and function body as per your requirement.
event.key() == QtCore.Qt.Key_Escape
is being used under the keyPresssEvent() function. Here, key_Escape is used to mention that the key we are going to generate event is the Escape key.
尝试:
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
print("Escape pressed")
我添加了这一行 self.dock_widget.setFocusPolicy(QtCore.Qt.StrongFocus),现在可以正常工作了。