Huy Tran
Huy Tran

Reputation: 41

Change the Cut, Copy, Paste shortcut to the custom slots in Qt 5

Currently, I am working on an editor program. I want to assign three shortcut keys (QKeySequence::Cut), (QKeySequence::Copy) and (QKeySequence::Paste) to my customized functions. However, it does not work as my expectation.

For testing, I open the "Application Example" from QtCreator. Then, I try to disable all the shortcut keys as following:

//cutAct->setShortcuts(QKeySequence::Cut);    
connect(cutAct, SIGNAL(triggered()), textEdit, SLOT(cut()));

copyAct = new QAction(QIcon(":/images/copy.png"), tr("&Copy"), this);
//copyAct->setShortcuts(QKeySequence::Copy);

connect(copyAct, SIGNAL(triggered()), textEdit, SLOT(copy()));

pasteAct = new QAction(QIcon(":/images/paste.png"), tr("&Paste"), this);
//pasteAct->setShortcuts(QKeySequence::Paste);

Surprisingly, the shortcut keys still working as before.

Another test is that:

  1. connect all the actions to customized slots.
  2. assign all the key sequences to the actions which have already connected to my customized slots.

Then, my result is

  1. Click Cut, Copy, Paste on toolbar go to my customized slots.
  2. The shortcut key works independently with my customized slots.

Any advice are welcome. Thank you very much.

Upvotes: 3

Views: 1816

Answers (1)

Huy Tran
Huy Tran

Reputation: 41

I have found the way to override the default shortcut, thanks to the code of Sigil.

I use the following code:

Delacre a new action in header:

  QShortcut &m_Paste1;

Then, in the constructor of class:

m_Paste1(*(new QShortcut(QKeySequence(QKeySequence::Paste), this, 0, 0, Qt::WidgetShortcut))),

Finally, connect it to your own slot

connect(&m_Paste1, SIGNAL(activated()), this, SLOT(paste()));

Upvotes: 1

Related Questions