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
This commit is contained in:
@@ -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 );
|
||||
|
@@ -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<QKeySequence> 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<QKeySequence> 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 )
|
||||
{
|
||||
|
Reference in New Issue
Block a user