Woltan
Woltan

Reputation: 14023

How to handle actions from context menu of a QLineEdit in Qt?

I would like to intercept the paste action of a QLineEdit context menu that is created by default in any QLineEdit widget (see picture below)

enter image description here

Is there a way to redirect the Paste action of the context menu by any means?

Upvotes: 0

Views: 3086

Answers (1)

Woltan
Woltan

Reputation: 14023

One can fiddle with the actions in the context menu by overloading the contextMenuEvent of the QLineEdit widget.

Edit:

The code of the link above:

void LineEdit::contextMenuEvent(QContextMenuEvent *event)
{
    QMenu *menu = createStandardContextMenu();
    menu->addAction(tr("My Menu Item"));
    //...
    menu->exec(event->globalPos());
    delete menu;
}

And the code that I actually used for my purposes:

menu = self.createStandardContextMenu()

menu.actions()[5].connect(self.paste) # The hard ref to the 6th item is not ideal but what can you do...

menu.exec_(event.globalPos())

Upvotes: 2

Related Questions