Question

i'm reading a book about Qt, there is an example about how to pop up the history menu, the text says "when this method is invoked it pops up a menu whose items correspond to the web pages the user has visited",

but i don't really see how we make the "menu" visible : here's the SLOT method:

void BrowerWindow::popupHistoryMenu()
{
    QMenu menu;
    …
    QListIterator<QWebHistoryItem> i(webView->history()->items());
    i.toBack();
    while(i.hasPrevious() && … )
    {
        const QWebHistoryItem & item = i.previous();
        QString title = ...
        QAction *action = new QAction(item.icon(), title, &menu);
        action->setData(item.url());
        menu.addAction(action);
    }
    AQP::accelerateMenu(&menu);
    if (QAction *action = menu.exec(QCursor::pos()))
        webView->load(action->data().toUrl());
}

So i don't see how the menu can be popped up from that simple function? ( "when this method is invoked it pops up a menu" )

Thanks

Was it helpful?

Solution

Exactly as the docs for QMenu (specifically, QMenu.exec()) say it works?

http://doc.qt.io/qt-5/qmenu.html#exec-2

Pops up the menu so that the action action will be at the specified global position p.

Relevant line in the code you posted:

if (QAction *action = menu.exec(QCursor::pos()))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top