3

i am adding QAction to QTableWidget as

m_pCutAction = new QAction( "Cut",this );
    m_pCutAction->setIcon(QIcon(":/Img/cut.png"));
    m_pCutAction->setShortcut(Qt::CTRL + Qt::Key_X);
 connect(m_pCutAction, SIGNAL(triggered()), this, SLOT(cut()));

 m_pCopyAction = new QAction( "Copy",this );
    m_pCopyAction->setIcon(QIcon(":/Img/copy.png"));
    m_pCopyAction->setShortcut(Qt::CTRL + Qt::Key_C);

and in contextMenu

void contextMenuEvent(QContextMenuEvent *event)
{
QScopedPointer<QMenu>menu(new QMenu());
    menu->addAction(m_pCutAction);
    menu->addAction(m_pCopyAction);
    menu->addAction(m_pPasteAction);
    menu->exec(event->globalPos());
 QTableWidget::contextMenuEvent(event);
}

but the action is not executing for Ctrl + X but it is executing from Ctrl +C. Copy operation i can perform but Cut operation i cant perform using keys.

i tried

m_pCutAction ->setShortcut(QKeySequence::Cut);

but this is also not helping me. same for paste and delete. only Copy operation i can do.

Wagmare
  • 1,354
  • 1
  • 24
  • 58
  • 1
    are you sure the other shortcuts do not get grabbed by the `parent()` widget? and so on (`parent()->parent()`). look at `QWidget::grabKeyboard()` – bibi Feb 18 '16 at 07:23
  • thanks bibi for the reply. but im not getting it. why only for ctrl+C is working and rest is not working. i guess it will work only in qt 4+ version. – Wagmare Feb 18 '16 at 09:18
  • im using new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_X), this, SLOT(cut())); option right now . but still not able to figure out the problem – Wagmare Feb 18 '16 at 09:18
  • I was just wondering if some these shortcuts are already defined in parent (or ancestor!) they might get filtered and cannot reach your widget. – bibi Feb 18 '16 at 09:56
  • your point makes sense .but im not doing anything on Parent widget. it is just a QWidget. EntryMainWidget::EntryMainWidget(QWidget *parent) : QWidget(parent) { m_pTableWidget = new TableWidget(this); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(m_pTableWidget); this->setLayout(mainLayout); } – Wagmare Feb 18 '16 at 10:07
  • ok, did you try to use the `m_pTableWidget->grabKeyboard()` ? – bibi Feb 18 '16 at 11:04
  • thanks bibi . yes i tried but in vain . – Wagmare Feb 18 '16 at 11:59
  • what about reimplementing the `void keyPressEvent (QKeyEvent *);` ? – bibi Feb 18 '16 at 12:35
  • im using new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_X), this, SLOT(cut())); it is working . – Wagmare Feb 18 '16 at 12:38
  • and keypress event also not giving the keys event for Ctrt+x and Ctrl+V . im surprised. – Wagmare Feb 18 '16 at 12:41

0 Answers0