DfrDkn
DfrDkn

Reputation: 1380

Pop up menu in QT

I have added a widget as a label (image or icon) to QT Main window.I need to display a pop up menu after I click (left or right click) on the label.Help please

Upvotes: 0

Views: 1373

Answers (1)

ak.
ak.

Reputation: 3449

reimplement mousePressEvent

void YourWidget::mousePressEvent(QMouseEvent *event)
{
    QMenu menu(this);
    QAction *action = menu.addAction("action");
    if (menu.exec(event->pos()) == action) {
        QMessageBox::information(this, "Next time I promise to google for the answers before asking");
    }
}

you may also use event filters to handle the mouse click events from the widgets that you dont derive from, i.e. if you have q plain QLabel, and you want to add a context menu to it. Read up in assistant about QObject::installEventFilter

Upvotes: 2

Related Questions