PyQt6 Control keyboard shortcut not working
-
I cannot get my application to recognize the control key. I have two
QTreeViews
in a group, and someQPushButtons
in a group. I am usingPushButton.setShortcut()
to attach a shortcut. Other keys seem to work fine. It's just the modifiers (ctrl, shift, alt) that don't. This is what I've tried:
QPushButton.setShortcut('ctrl')
QPushButton.setShortcut(QKeySequence('ctrl'))
QPushButton.setShortcut(QKeySequence(0x01000021)) # ctrl key
QPushButton.setShortcut(QKeySequence(0x04000000)) # modifier
I've also tried with members. In reality, I want a key combination. But, to get there I need to get my app to recognize ctrl first. QTreeViews' shortcuts work fine, including those that use ctrl. So, I know it's not an OS (Fedora KDE Plasma) issue.
Any ideas? Are my trees disabling ctrl? I don't understand why a widget would do that, nor do I think it would.
-
Hi,
Ctrl is not a key, it's a modifier. You cannot make a key sequence out of it alone. It must be combined with another key (shift is also a modifier and not a key).
-
Cool, thanks. However, I have tried to combine Ctrl with F1 (the combo I actually want to use) via
setShortcut('Ctrl+F1')
, and it does not work. Also, I have attempted to grab hold of something byprint(shortcut().toString())
to see, and all modifiers print human readable text except Ctrl (an empty string). Now, printingshortcut()
always prints aQKeySequence
object, but no modifiers work. It could be the way I combine modifiers (+
or|
), but I can't find any reference on how to do it any other way.Something funny is going on. It must be my understanding, but I've hit a wall.
-
Cool, thanks. However, I have tried to combine Ctrl with F1 (the combo I actually want to use) via
setShortcut('Ctrl+F1')
, and it does not work. Also, I have attempted to grab hold of something byprint(shortcut().toString())
to see, and all modifiers print human readable text except Ctrl (an empty string). Now, printingshortcut()
always prints aQKeySequence
object, but no modifiers work. It could be the way I combine modifiers (+
or|
), but I can't find any reference on how to do it any other way.Something funny is going on. It must be my understanding, but I've hit a wall.
Which platform are you on ?
Can you provide a minimal script that shows this behaviour ? -
I am on Fedora 38 KDE Plasma. Further testing reveals that 'Shift+F1' works, and 'Alt+<anything>' does not work as it is used by my OS (opens "start menu").
I really appreciate that you want to help, but I just don't feel like extracting the relevant bits from my multi-module project. So, I accept that you may drop me like a hot potato now, but I can always use 'Shift+F1'. Not what I want, and I have no clue why Qt is behaving this way for future reference, but it will get the job done.
If my OS points you to an idea, I'm all ears. But, I'm not on a Mac.
-
I just thought of something. I am using a subclassed
QMainWindow
, and I read while troubleshooting (which I've done a lot of) that Ctrl is tied to the menubar of aQMainWindow
. I don't know if that's relevant, but it's worth testing as long as I can accept the extra code required to implement a QAction that has a signal emitted by myclicked
signal.Thoughts on
QMainWindow
's blocking modifiers? I don't lean that way, but shortcuts are new to me. -
I am on Fedora 38 KDE Plasma. Further testing reveals that 'Shift+F1' works, and 'Alt+<anything>' does not work as it is used by my OS (opens "start menu").
I really appreciate that you want to help, but I just don't feel like extracting the relevant bits from my multi-module project. So, I accept that you may drop me like a hot potato now, but I can always use 'Shift+F1'. Not what I want, and I have no clue why Qt is behaving this way for future reference, but it will get the job done.
If my OS points you to an idea, I'm all ears. But, I'm not on a Mac.
Did you check the keyboard shortcuts configuration ?
You might be using inadvertently something that is already taken.KDE has quite an extensive list of shortcuts to allow for almost mouse less usage.
-
Did you check the keyboard shortcuts configuration ?
You might be using inadvertently something that is already taken.KDE has quite an extensive list of shortcuts to allow for almost mouse less usage.
@SGaist I did. The combo is not spoken for. I'll double check though. I've troubleshot a lot, and can't recall all I've tried to detail in a post here. It still wouldn't explain why I can't get the Ctrl key to print, while other modifiers do.
There might not be much people here can do unless I man up and post an MRE.
EDIT: Oops! You are right @SGaist . The OS shortcut slipped under the cracks. Alt+F1 is assigned to KWin, and it blocks my app from using it. Damn, wish I would have not missed that earlier—would have saved hours of troubleshooting.
-
M Malonn has marked this topic as solved on