Reputation: 263
I am trying to bind a key directly to a method call. I have found this solution but it binds the key to a button:
How do I make a shortcut using arrow key with PySide2?
When I try to bind it to a method like this:
QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.LeftArrow), self.my_function)
Errors are thrown:
TypeError: 'PySide2.QtWidgets.QShortcut' called with wrong argument types:
PySide2.QtWidgets.QShortcut(QKeySequence, method)
Supported signatures: ......
Code example:
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_())
Corresponding ui-file:
<?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>
Upvotes: 2
Views: 855
Reputation: 244132
A QShorcut is associated with a QWidget, therefore it throws you that error, you must also use "left" in the QKeySequence since QKeySequence does not support Qt::Key alone but with modifiers.
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_())
Upvotes: 2