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. [SOLVED] Context menu for QListView entries
QtWS25 Last Chance

[SOLVED] Context menu for QListView entries

Scheduled Pinned Locked Moved General and Desktop
qlistviewqmenu
5 Posts 4 Posters 8.9k Views
  • 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.
  • M Offline
    M Offline
    mkolenda
    wrote on last edited by mkolenda
    #1

    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 )

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi!
      Maybe what you want isn't an item specific context menu but a custom item delegate (see: http://doc.qt.io/qt-5/model-view-programming.html#delegate-classes).

      M 1 Reply Last reply
      0
      • ? A Former User

        Hi!
        Maybe what you want isn't an item specific context menu but a custom item delegate (see: http://doc.qt.io/qt-5/model-view-programming.html#delegate-classes).

        M Offline
        M Offline
        mkolenda
        wrote on last edited by mkolenda
        #3

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sithlord33
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi and welcome to devnet,

            Please don't necropost such old thread.

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            3

            • Login

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