我正在尝试将键直接绑定到方法调用。我找到了此解决方案,但将键绑定到了按钮:
How do I make a shortcut using arrow key with PySide2?
当我尝试将其绑定到这样的方法时:
QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.LeftArrow), self.my_function)
引发错误:
TypeError: 'PySide2.QtWidgets.QShortcut' called with wrong argument types:
PySide2.QtWidgets.QShortcut(QKeySequence, method)
Supported signatures: ......
代码示例:
from PySide2 import QtCore, QtWidgets, QtUiTools, QtGui
import sys
class mainW(QtCore.QObject):
def __init__(self):
super(mainW, self).__init__()
self.init_ui()
self.init_connections()
self.ui.show()
def init_ui(self):
file = QtCore.QFile("ressource_files/energiebericht_pruefung_main.ui")
file.open(QtCore.QFile.ReadOnly)
loader = QtUiTools.QUiLoader()
self.ui = loader.load(file)
def init_connections(self):
QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.LeftArrow), self.my_function)
def my_function(self):
print('* MY FUNCTION HAS BEEN CALLED *')
# MAIN
app = QtWidgets.QApplication(sys.argv)
w = mainW()
sys.exit(app.exec_())
对应的ui文件:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1394</width>
<height>726</height>
</rect>
</property>
<property name="windowTitle">
<string>Energiebericht Prüfung</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="maximumSize">
<size>
<width>200</width>
<height>16777215</height>
</size>
</property>
<property name="title">
<string>Infrastrukturelement</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QListWidget" name="lwISE"/>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<widget class="QGroupBox" name="groupBox_2">
<property name="maximumSize">
<size>
<width>200</width>
<height>16777215</height>
</size>
</property>
<property name="title">
<string>Profil</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QListWidget" name="lwProfil"/>
</item>
</layout>
</widget>
</item>
<item row="0" column="2">
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Chart</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<widget class="QStackedWidget" name="stackedWidget">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="gfx1"/>
<widget class="QWidget" name="gfx2"/>
</widget>
</item>
</layout>
</widget>
</item>
<item row="1" column="0" colspan="3">
<widget class="QTableWidget" name="tW">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>140</height>
</size>
</property>
<property name="rowCount">
<number>3</number>
</property>
<property name="columnCount">
<number>13</number>
</property>
<row/>
<row/>
<row/>
<column/>
<column/>
<column/>
<column/>
<column/>
<column/>
<column/>
<column/>
<column/>
<column/>
<column/>
<column/>
<column/>
</widget>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1394</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
答案 0 :(得分:1)
QShorcut与QWidget相关联,因此会引发该错误,您还必须在QKeySequence中使用“ left”,因为QKeySequence不仅仅支持Qt :: Key,而是带有修饰符。
import os
import sys
from PySide2 import QtCore, QtWidgets, QtUiTools, QtGui
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
class mainW(QtCore.QObject):
def __init__(self):
super(mainW, self).__init__()
self.init_ui()
self.init_connections()
self.ui.show()
def init_ui(self):
filename = os.path.join(
CURRENT_DIR, "ressource_files/energiebericht_pruefung_main.ui"
)
file = QtCore.QFile(filename)
if not file.open(QtCore.QFile.ReadOnly):
sys.exit(-1)
loader = QtUiTools.QUiLoader()
self.ui = loader.load(file)
def init_connections(self):
QtWidgets.QShortcut(QtGui.QKeySequence("left"), self.ui, self.my_function)
@QtCore.Slot()
def my_function(self):
print("* MY FUNCTION HAS BEEN CALLED *")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = mainW()
sys.exit(app.exec_())