Important: Please read the Qt Code of Conduct -

Context menu in QListWidget - reference to the item

  • Hi,
    I have problem with context menu. I have created context menu in QListWidget, and now menu shows after mouse right clicking. I have some QActions there and I want to call the proper action for the the item that menu was showed.
    I hope I explain it clearly.

    This is piece of my code to show context menu:
    void MyClass::on_listWidget_customContextMenuRequested(const QPoint& pos)
    QListWidgetItem* widget = ui->listWidget->itemAt(pos);
    if(widget != 0)
    int row = ui->listWidget->row(widget);
    QString text = widget->text();
    contextMenu_ = new QMenu(ui->listWidget);
    if(pos != QPoint(0,0))
    QPoint pointGlobal;
    pointGlobal = mapToGlobal(pos);

            QAction* menuItem1 = new QAction("action 1", 0);
            connect(menuItem1, SIGNAL(triggered()),
                    this, SLOT(getContextMenuAction
            contextMenu_ -> addAction(menuItem1);
            QAction* menuItem2 = new QAction("action 2", 0);
            connect(menuItem2, SIGNAL(triggered()),
                    this, SLOT(getContextMenuAction()));
            contextMenu_ -> addAction(menuItem2);


    In that way I get menu and then after clicking on action getContextMenuAction method is called. But in the same moment I loose info on which item of the list the menu was clicked (I need that info). Is there any way to get that info?

Log in to reply