how to set "Ctrl + c" for copy shortcut on a QMainWindow?
-
Hi, I am working on a QMainWindow and need to catch "ctrl +c" to operate copy of some data on it.
class MainWindow(QMainWindow): def __init__(self) -> None:
I have made a menuBar and add QAction with "Ctrl + c" shortcut to it:
def _create_menu(self): file_menu = self.menuBar().addMenu("&File") copy_data = QAction(text="&Copy", parent=self) #copy_data.setShortcut("Ctrl+c") copy_data.setShortcut(Qt.CTRL + Qt.Key_C) copy_data.triggered.connect(self._on_copy_data) file_menu.addAction(copy_data)
and
create my functiondef _on_copy_data(self): self._bits_widget.copy()
But when I pressed on "Ctrl+c" _on_copy_data function didn't triggered.
When I use the menu bar and click on copy it's will be triggered.Where is my mistake when I use other combine of keys like "Ctrl + l" it will work also.
I use Pyqt5 in ubuntu OS
Thanks -
Hello,
I'm not sure about whether it's the best idea or not, but I personally do it this way:
QAction* lInfoRowAction = new QAction("info", this);
lInfoRowAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_I));
addAction(lInfoRowAction);
connect(lInfoRowAction, SIGNAL(triggered()), this, SLOT(showInfoGeneral()));Directly in a function in a class that inherits QMainWindow. This way, the shortcut will be global to the window.
-
@Herzl-Sh said in how to set "Ctrl + c" for copy shortcut on a QMainWindow?:
How can I to unfocus them?
-
Because we had declarations of shortcuts littered all over the place we came up with the following solution to handle them in one place.
We derived a class
MyApplication
fromQApplication
and overrodebool notify(QObject *receiver, QEvent *e) override
. Within this function we checkede->type() == QEvent::Shortcut
. Everything that couldn't be handled is then forwarded toQApplication::notify
.We didn't override the Ctrl+C shortcut or anything similar. However, because this is so early in Qt's signal handling you could be lucky with this approach.