#include <QWidget>
#include <QMenu>
#include <QAction>
#include <QList>
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = nullptr)
: QWidget(parent)
{
QMenu *contextMenu = new QMenu(this);
QAction *action1 = new QAction("Action 1", this);
QAction *action2 = new QAction("Action 2", this);
QAction *action3 = new QAction("Action 3", this);
m_actions.append(action1);
m_actions.append(action2);
m_actions.append(action3);
for (QAction *action : m_actions)
{
contextMenu->addAction(action);
}
this->setContextMenuPolicy(Qt::ActionsContextMenu);
this->addActions(m_actions);
}
private:
QList<QAction*> m_actions;
};