QListView ContextMenu



  • Hi,
    I have a QListView and I wanted to set its context menu so I started with delete action. I need to get the selected item's data to be able to delete the item from the model. but when I do:

    void Music::albumContextMenu()
    {
    QAction *deleteAction = new QAction("Delete",ui->albumView);

    ui->albumView->addAction(deleteAction);
    ui->albumView->setContextMenuPolicy(Qt::ActionsContextMenu);
    
    connect(deleteAction,SIGNAL(triggered(bool)),SLOT(albumDeleteAction()));
    

    }

    void Music::albumDeleteAction()
    {
    QModelIndex index= ui->albumView->selectedIndexes();
    int indexNumber = index.data(Qt::UserRole+1).toInt();
    ...
    ...
    }

    But the selectedIndexes() function is private.
    How should I do this?
    is there a better way to do this?


  • Moderators

    You should go through the selection model:

    QModelIndexList indexes = ui->albumView->selectionModel()->selectedIndexes();
    if(!indexes.isEmpty())
    {
       int indexNumber = indexes.front().data(Qt::UserRole+1).toInt();
       ...
    }
    


  • Hi,
    Thanks it worked


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.