Custom context menu item
-
I want to add a context menu item that will execute a function. The context menu appears after right clicking a QPlainTextEdit.
-
@Grand_Titan
There's an alternative that doesn't require subclassing.First set the policy:
yourPlainText->setContextMenuPolicy(Qt::CustomContextMenu);
then connect your main window to receive the customContextMenuRequested signal.In a slot in your main window you can do like:
showContextMenu(const QPoint &) { QMenu* menu=yourPlainText->createStandardContextMenu(); menu->addSeparator(); // create your action QAction* action=menu->addAction(tr("your action")); // then connect your action to a slot in your main window connect(action, ....); menu->exec(QCursor::pos()); menu->deleteLater(); }
-
You have to override customContextMenuEvent().
-
@Christian-Ehrlicher How can I do that?
-
@Grand_Titan
How do you mean? Override the method, put whatever code you want it to execute in the override. Simple example code is in the linked page, what do you want to achieve beyond what it shows you to do? -
This post is deleted!
-
@JonB I'm new to c++ and I'm still not quite familiar with subclassing and overriding methods. Also I'm not sure how to do it because I already created my QPlainTextEdit in the editor and made its style and a lot of my program depends on its existence. Im not sure how to account for all of that while doing something like an override. Is it possible to do something like this with an event filter? Because I already have an event filter installed on this object.
-
@Grand_Titan
In the longer run you absolutely must get familiar with C++ subclassing/overriding! And there will be times in Qt where you cannot get away without this.It is a problem if you use the Qt base classes in Designer because as you note, you then must subclass in order to do certain things. Read about how Qt Creator allows you to promote widgets you have subclassed so you can still set them from Designer.
For this occasion, QEvent::type has
QEvent::ContextMenu 82 Context popup menu (QContextMenuEvent).
so try using that in installed filter on existing
QPlainTextEdit
to achieve without subclassing? -
@Grand_Titan
There's an alternative that doesn't require subclassing.First set the policy:
yourPlainText->setContextMenuPolicy(Qt::CustomContextMenu);
then connect your main window to receive the customContextMenuRequested signal.In a slot in your main window you can do like:
showContextMenu(const QPoint &) { QMenu* menu=yourPlainText->createStandardContextMenu(); menu->addSeparator(); // create your action QAction* action=menu->addAction(tr("your action")); // then connect your action to a slot in your main window connect(action, ....); menu->exec(QCursor::pos()); menu->deleteLater(); }
-
@mpergand i would change juste one thing: make the QMenu a stack object since you are using exec. Hence no need for explicit deletion laterEdit: misread the first line, that's the correct way to include the default menu.
-
Thank you all. I think ill be able to achieve this now. Also thanks for the suggestion I will dig deeper into that stuff.
-