From 3a918864f9bccf107e6390d7c2a37fa5c2db755d Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 27 Sep 2019 13:44:55 +0200 Subject: [PATCH] Make accelerators work for menu items created in the code too The previous commit fixed accelerators support in wxQt for the items created in XRC, but not for those created directly in the code, as wxMenuItem::SetItemLabel() is not called in this case. Refactor the code to extract UpdateShortcutsFromLabel() from SetItemLabel() and call the new function both from there and from wxMenuItem ctor, to ensure that the accelerators are taken into account in any case. This commit is best viewed with "git diff --color-moved". See https://github.com/wxWidgets/wxWidgets/pull/1544 --- include/wx/qt/menuitem.h | 3 ++- src/qt/menuitem.cpp | 36 +++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/include/wx/qt/menuitem.h b/include/wx/qt/menuitem.h index c91a16a4e1..24e5e27003 100644 --- a/include/wx/qt/menuitem.h +++ b/include/wx/qt/menuitem.h @@ -12,6 +12,7 @@ #include "wx/bitmap.h" class QAction; +class wxQtAction; class WXDLLIMPEXP_FWD_CORE wxMenu; @@ -41,7 +42,7 @@ public: private: // Qt is using an action instead of a menu item. - QAction *m_qtAction; + wxQtAction *m_qtAction; wxBitmap m_bitmap; wxDECLARE_DYNAMIC_CLASS( wxMenuItem ); diff --git a/src/qt/menuitem.cpp b/src/qt/menuitem.cpp index f554294a7d..9471909fa1 100644 --- a/src/qt/menuitem.cpp +++ b/src/qt/menuitem.cpp @@ -25,6 +25,10 @@ public: wxQtAction( wxMenu *parent, int id, const wxString &text, const wxString &help, wxItemKind kind, wxMenu *subMenu, wxMenuItem *handler ); + // Set the action shortcut to correspond to the accelerator specified by + // the given label. + void UpdateShortcutsFromLabel(const wxString& text); + private: void onActionTriggered( bool checked ); }; @@ -51,19 +55,7 @@ void wxMenuItem::SetItemLabel( const wxString &label ) { wxMenuItemBase::SetItemLabel( label ); -#if wxUSE_ACCEL - QString qlabel = wxQtConvertString( label ); - int index = qlabel.lastIndexOf( QChar( '\t' ) ); - - if ( index != -1 ) - { - QList shortcuts = m_qtAction->shortcuts(); - QString shortcut_key = qlabel.remove( 0, index+1 ); - - shortcuts.append( QKeySequence( shortcut_key ) ); - m_qtAction->setShortcuts( shortcuts ); - } -#endif // wxUSE_ACCEL + m_qtAction->UpdateShortcutsFromLabel( label ); m_qtAction->setText( wxQtConvertString( label )); } @@ -175,8 +167,26 @@ wxQtAction::wxQtAction( wxMenu *parent, int id, const wxString &text, const wxSt } connect( this, &QAction::triggered, this, &wxQtAction::onActionTriggered ); + + UpdateShortcutsFromLabel( text ); } +void wxQtAction::UpdateShortcutsFromLabel(const wxString& text) +{ +#if wxUSE_ACCEL + QString qlabel = wxQtConvertString( text ); + int index = qlabel.lastIndexOf( QChar( '\t' ) ); + + if ( index != -1 ) + { + QList shortcuts = m_qtAction->shortcuts(); + QString shortcut_key = qlabel.remove( 0, index+1 ); + + shortcuts.append( QKeySequence( shortcut_key ) ); + setShortcuts( shortcuts ); + } +#endif // wxUSE_ACCEL +} void wxQtAction::onActionTriggered( bool checked ) {