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
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Context menu for QListView entries

Scheduled Pinned Locked Moved General and Desktop
qlistviewqmenu
5 Posts 4 Posters 9.0k Views 3 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.
  • M Offline
    M Offline
    mkolenda
    wrote on 31 Jul 2015, 11:01 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 31 Jul 2015, 11:51 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 31 Jul 2015, 15:49
      0
      • ? A Former User
        31 Jul 2015, 11:51

        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 31 Jul 2015, 15:49 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 29 Nov 2019, 07:20 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 29 Nov 2019, 07:29 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