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. Adding contextmenu for QTreeWidgetItem .
QtWS25 Last Chance

Adding contextmenu for QTreeWidgetItem .

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 6.8k 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.
  • B Offline
    B Offline
    BibekKumar
    wrote on 25 Sept 2012, 11:04 last edited by
    #1

    Inside constructor

    @connect(ui.UserSpecificMaterial_treeWidget, SIGNAL(customContextMenuRequested(const QPoint &)),this, SLOT(ContMenu(const QPoint &)));@

    Inside slot

    @void MyContMenu::ContMenu(const QPoint &pos)
    

    {
    QTreeWidgetItem *item = ui.UserSpecificMaterial_treeWidget->itemAt(pos);
    if (!item)
    return;
    QMenu *menu = new QMenu(ui.UserSpecificMaterial_treeWidget);
    myAction = menu->addAction("Remove");
    myAction->setIcon(QIcon(QString::fromUtf8("Resources/Remove.png")));
    myAction->setShortcut(tr("Ctrl+D"));
    myAction->setStatusTip(tr("Remove the respective material from the User DB"));
    menu->exec(ui.UserSpecificMaterial_treeWidget->viewport()->mapToGlobal(pos));
    /---code to remove the item ./
    }
    @
    In the above code whenever i right click on the QTreeWidgetItem it will show me the contextmenu having a single menuitem named Remove . What i want is whenever the user click on that remove menuitem at that time only it should remove that QTreeWidgetItem from the qtreewidget . but in the above code after rigtclick even if i click on any part of the UI it removes the respective QTreeWidgetItem from the treewidget , which i want to avoid .

    Thankss in advance .

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mohsen
      wrote on 26 Sept 2012, 07:28 last edited by
      #2

      doesnt' QTreeWidget::itemAt (const QPoint & p) helped? you only need to have it kept on private pointer then use it when QMenuItem has triggered().

      1 Reply Last reply
      0
      • B Offline
        B Offline
        bhushan.dhawde
        wrote on 15 Dec 2014, 13:48 last edited by
        #3

        Try This:
        @
        QTreeWidgetItem *item = ui->fileList->itemAt(pos);
        if (!item)
        return;
        QMenu menu = new QMenu(ui->fileList);
        QAction
        myAction = menu->addAction("Remove");
        myAction->setIcon(QIcon(QString::fromUtf8("Resources/Remove.png")));
        myAction->setShortcut(tr("Ctrl+D"));
        myAction->setStatusTip(tr("Remove the respective material from the User DB"));
        myAction = menu->exec(ui->fileList->viewport()->mapToGlobal(pos));
        if(myAction == NULL) return;
        on_deletefile_clicked();
        @

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DBoosalis
          wrote on 16 Dec 2014, 02:50 last edited by
          #4

          Or make it a signal in your QTreeView like the following:

          @void MyTreeView::mousePressEvent(QMouseEvent *me)
          {
          QModelIndex index;
          if (me->button() == Qt::RightButton) {
          index = indexAt(me->pos());
          if (index.isValid()) {
          emit doPopup(index,me->globalPos());
          }
          }
          else
          QTreeView::mousePressEvent(me);
          }
          @
          Connect the signal to your class:
          @ connect(myFieldsTree,SIGNAL(doPopup(const QModelIndex &,const QPoint &)),this,SLOT(popupMenuSlot(const QModelIndex &,const QPoint &)));
          @
          Then in your slot loooks like this:

          void MessageWindow::popupMenuSlot(const QModelIndex &,const @QPoint &p)
          {
          popupMenu->popup(p);
          }@

          1 Reply Last reply
          0
          • D Offline
            D Offline
            DBoosalis
            wrote on 16 Dec 2014, 02:50 last edited by
            #5

            Or make it a signal in your QTreeView like the following:

            @void MyTreeView::mousePressEvent(QMouseEvent *me)
            {
            QModelIndex index;
            if (me->button() == Qt::RightButton) {
            index = indexAt(me->pos());
            if (index.isValid()) {
            emit doPopup(index,me->globalPos());
            }
            }
            else
            QTreeView::mousePressEvent(me);
            }
            @
            Connect the signal to your class:
            @ connect(myFieldsTree,SIGNAL(doPopup(const QModelIndex &,const QPoint &)),this,SLOT(popupMenuSlot(const QModelIndex &,const QPoint &)));
            @
            Then in your slot loooks like this:

            void MessageWindow::popupMenuSlot(const QModelIndex &,const @QPoint &p)
            {
            popupMenu->popup(p);
            }@

            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