@@ -70,24 +70,57 @@ def possibly_static_exec_(cls, *args, **kwargs):
7070 return cls .exec_ (* args , ** kwargs )
7171
7272
73+ def set_shortcut (self , shortcut , old_set_shortcut ):
74+ """Ensure that the type of `shortcut` is compatible to `QAction.setShortcut`."""
75+ from qtpy .QtCore import Qt
76+ from qtpy .QtGui import QKeySequence
77+
78+ if isinstance (shortcut , (QKeySequence .StandardKey , Qt .Key , int )):
79+ shortcut = QKeySequence (shortcut )
80+ old_set_shortcut (self , shortcut )
81+
82+
83+ def set_shortcuts (self , shortcuts , old_set_shortcuts ):
84+ """Ensure that the type of `shortcuts` is compatible to `QAction.setShortcuts`."""
85+ from qtpy .QtCore import Qt
86+ from qtpy .QtGui import QKeySequence
87+
88+ if isinstance (
89+ shortcuts ,
90+ (QKeySequence , QKeySequence .StandardKey , Qt .Key , int , str ),
91+ ):
92+ shortcuts = (shortcuts ,)
93+
94+ shortcuts = tuple (
95+ (
96+ QKeySequence (shortcut )
97+ if isinstance (shortcut , (QKeySequence .StandardKey , Qt .Key , int ))
98+ else shortcut
99+ )
100+ for shortcut in shortcuts
101+ )
102+ old_set_shortcuts (self , shortcuts )
103+
104+
73105def add_action (self , * args , old_add_action ):
74106 """Re-order arguments of `addAction` to backport compatibility with Qt>=6.3."""
75- from qtpy .QtCore import QObject
107+ from qtpy .QtCore import QObject , Qt
76108 from qtpy .QtGui import QIcon , QKeySequence
77109
78110 action : QAction
79111 icon : QIcon
80112 text : str
81- shortcut : QKeySequence | QKeySequence .StandardKey | str | int
113+ shortcut : QKeySequence | QKeySequence .StandardKey | Qt . Key | str | int
82114 receiver : QObject
83115 member : bytes
116+
84117 if all (
85118 isinstance (arg , t )
86119 for arg , t in zip (
87120 args ,
88121 [
89122 str ,
90- (QKeySequence , QKeySequence .StandardKey , str , int ),
123+ (QKeySequence , QKeySequence .StandardKey , Qt . Key , str , int ),
91124 QObject ,
92125 bytes ,
93126 ],
@@ -105,16 +138,15 @@ def add_action(self, *args, old_add_action):
105138 text , shortcut , receiver , member = args
106139 action = old_add_action (self , text , receiver , member , shortcut )
107140 else :
108- return old_add_action (self , * args )
109- return action
110- if all (
141+ action = old_add_action (self , * args )
142+ elif all (
111143 isinstance (arg , t )
112144 for arg , t in zip (
113145 args ,
114146 [
115147 QIcon ,
116148 str ,
117- (QKeySequence , QKeySequence .StandardKey , str , int ),
149+ (QKeySequence , QKeySequence .StandardKey , Qt . Key , str , int ),
118150 QObject ,
119151 bytes ,
120152 ],
@@ -123,11 +155,11 @@ def add_action(self, *args, old_add_action):
123155 if len (args ) == 3 :
124156 icon , text , shortcut = args
125157 action = old_add_action (self , icon , text )
126- action .setShortcut (QKeySequence ( shortcut ) )
158+ action .setShortcut (shortcut )
127159 elif len (args ) == 4 :
128160 icon , text , shortcut , receiver = args
129161 action = old_add_action (self , icon , text , receiver )
130- action .setShortcut (QKeySequence ( shortcut ) )
162+ action .setShortcut (shortcut )
131163 elif len (args ) == 5 :
132164 icon , text , shortcut , receiver , member = args
133165 action = old_add_action (
@@ -136,12 +168,14 @@ def add_action(self, *args, old_add_action):
136168 text ,
137169 receiver ,
138170 member ,
139- QKeySequence ( shortcut ) ,
171+ shortcut ,
140172 )
141173 else :
142- return old_add_action (self , * args )
143- return action
144- return old_add_action (self , * args )
174+ action = old_add_action (self , * args )
175+ else :
176+ action = old_add_action (self , * args )
177+
178+ return action
145179
146180
147181def static_method_kwargs_wrapper (func , from_kwarg_name , to_kwarg_name ):
0 commit comments