Important: Please read the Qt Code of Conduct -

[SOLVED] Context menu for QListView entries

  • I know how to set Qt::CustomContextMenu for QListView. I want to make two different menus for QListView and for items inside of it.

    I mean, when user click item in QListView he may want to see something like properties or edit this entry.

    If he click on QListView beyond items he may want to add new item to this list.

    How to make different menu for item? ( and know what item it was )

  • Hi!
    Maybe what you want isn't an item specific context menu but a custom item delegate (see:

  • Working code:

    void MainWindow::listWidgetContextMenu(const QPoint &pos)
        QPoint globalpos = ui->listWidget->mapToGlobal(pos);
        QMenu menuBeyondItem;
        QAction* action_addElement = menuBeyondItem.addAction("Add");
        QMenu menuForItem;
        QAction* action_editElement = menuForItem.addAction("Edit");
        QListWidgetItem* pointedItem = ui->listWidget->itemAt(pos);
        QAction* selectedAction;
        if(!pointedItem) {
            selectedAction = menuBeyondItem.exec(globalpos);
            if(selectedAction) {
                if(selectedAction == action_addElement) {
                    qDebug() << "Add";
        else {
            selectedAction = menuForItem.exec(globalpos);
            if(selectedAction) {
                if(selectedAction == action_editElement) {
                    qDebug() << "Edit";

    What I didn't know was existence of QListWidget::itemAt(QPoint) function.

    For QListView use QModelIndex QListView::indexAt(const QPoint & p) const function.

  • This post is deleted!

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Please don't necropost such old thread.

    In any case, the point is provided by the customContextMenuRequested signal.

Log in to reply