Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListView ContextMenu
Forum Updated to NodeBB v4.3 + New Features

QListView ContextMenu

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shahriar25
    wrote on last edited by shahriar25
    #1

    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?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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();
         ...
      }
      
      1 Reply Last reply
      1
      • S Offline
        S Offline
        shahriar25
        wrote on last edited by
        #3

        Hi,
        Thanks it worked

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved