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. I want to right click the particular treewidgetitem..

I want to right click the particular treewidgetitem..

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 3 Posters 5.9k 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.
  • K Offline
    K Offline
    karti gesar
    wrote on last edited by
    #4

    treeWidget = new QTreeWidget(this);
    treeWidget->setGeometry(0,0,200,200);
    treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(treeWidget, SIGNAL(customContextMenuRequested(const QPoint&)),
    this, SLOT(ShowContextMenu(const QPoint&)));
    }

    void MainWindow::ShowContextMenu( const QPoint & pos )
    {

    QPoint globalPos = treeWidget->mapToGlobal(pos);
       // for QAbstractScrollArea and derived classes you would use:
       // QPoint globalPos = myWidget->viewport()->mapToGlobal(pos);
    
       QMenu myMenu;
       myMenu.addAction("Menu Item 1");
       // ...
    
       QAction* selectedItem = myMenu.exec(globalPos);
       if (selectedItem)
       {
           // something was chosen, do stuff
       }
       else
       {
           // nothing was chosen
       }
    

    }

    1 Reply Last reply
    1
    • K Offline
      K Offline
      karti gesar
      wrote on last edited by
      #5

      above code is what i used...still right click appear all area of the treewidget

      RatzzR 1 Reply Last reply
      1
      • K karti gesar

        above code is what i used...still right click appear all area of the treewidget

        RatzzR Offline
        RatzzR Offline
        Ratzz
        wrote on last edited by
        #6

        @karti-gesar
        something like this

            QModelIndex index = ui->treeWidget->indexAt(point);
            if(index .isValid())
            {
             /// add actions here
            }

        --Alles ist gut.

        1 Reply Last reply
        1
        • K Offline
          K Offline
          karti gesar
          wrote on last edited by
          #7

          i don't know how to use this

          RatzzR 1 Reply Last reply
          1
          • K karti gesar

            i don't know how to use this

            RatzzR Offline
            RatzzR Offline
            Ratzz
            wrote on last edited by
            #8

            @karti-gesar
            This works for me
            //cons

                ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
                connect(ui->treeWidget,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(goHere(QPoint)));
            

            //go here

                QMenu* menu = new QMenu("Menu",ui->treeWidget);
                QModelIndex index = ui->treeWidget->indexAt(point);
                if(index.isValid())
                {
                    QAction *myaction  = new QAction(QIcon(":/new/prefix1/Icons/action.png"),"action",this);
                    menu->addAction(myaction);
                    menu->exec((QCursor::pos()));
                }

            --Alles ist gut.

            1 Reply Last reply
            2
            • K Offline
              K Offline
              karti gesar
              wrote on last edited by
              #9

              ya it works ...but all the treewidget area getting right click

              RatzzR 1 Reply Last reply
              1
              • K karti gesar

                ya it works ...but all the treewidget area getting right click

                RatzzR Offline
                RatzzR Offline
                Ratzz
                wrote on last edited by
                #10

                @karti-gesar said:

                but all the treewidget area getting right click

                What do you mean?

                Show me your code

                --Alles ist gut.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  karti gesar
                  wrote on last edited by
                  #11

                  for particular item only i want right....but for entire widget it appears

                  1 Reply Last reply
                  1
                  • K Offline
                    K Offline
                    karti gesar
                    wrote on last edited by
                    #12

                    treeWidget = new QTreeWidget(this);

                    treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
                      treeWidget->setHeaderLabel("Display");
                    
                    
                      //tree = new QTreeView(this);
                           // tree->setModel(treeWidget);
                    
                      //Cpp file:
                    

                    myAction = new QAction(tr("&Add Subprogram"), treeWidget);
                    connect(myAction, SIGNAL(triggered()), this, SLOT(mySlot()));
                    myAction1 = new QAction(tr("&Delete Subprogram"), treeWidget);
                    connect(myAction1, SIGNAL(triggered()), this, SLOT(deleteSubpgm()));
                    myAction2 = new QAction(tr("&AddProgram"), treeWidget);
                    connect(myAction2, SIGNAL(triggered()), this, SLOT(AddProgram()));
                    myAction3 = new QAction(tr("&DeleteProgram"), treeWidget);
                    connect(myAction3, SIGNAL(triggered()), this, SLOT(DeleteProgram()));
                    // Then add it to your treeWidget:
                    treeWidget->addAction(myAction);
                    treeWidget->addAction(myAction1);
                    treeWidget->addAction(myAction2);
                    treeWidget->addAction(myAction3);
                    prg1 = new QTreeWidgetItem(treeWidget);
                    prg1->setIcon(0,QIcon("C:/Users/skarthick.DART0/Downloads/clapperboard.png"));
                    prg1->setText(0, tr("Program1"));
                    }

                    void Dialog::mySlot()
                    {
                    qDebug()<<"ee";
                    osloItem = new QTreeWidgetItem(prg1);
                    static int i=1;
                    osloItem->setText(0, QString("Subprogram %1").arg(i));

                    i++;
                    if(i>=12)
                    {
                        QMessageBox msgBox;
                        msgBox.setText("no more to add.");
                        msgBox.exec();
                    }
                    

                    }

                    void Dialog::deleteSubpgm()
                    {
                    osloItem = treeWidget->currentItem();
                    if(osloItem)
                    delete osloItem->parent()->takeChild(osloItem->parent()->indexOfChild(osloItem));

                    }
                    void Dialog::AddProgram()
                    {
                    prg2 = new QTreeWidgetItem(treeWidget);
                    static int j=2;
                    prg2->setIcon(0,QIcon("C:/Users/skarthick.DART0/Downloads/clapperboard.png"));
                    prg2->setText(0, QString("program %2").arg(j));
                    j++;

                    }
                    void Dialog::DeleteProgram()
                    {
                    prg2= treeWidget->currentItem();
                    if(prg2)
                    {

                    delete prg2->parent();

                    }
                    }

                    RatzzR 1 Reply Last reply
                    1
                    • K karti gesar

                      treeWidget = new QTreeWidget(this);

                      treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
                        treeWidget->setHeaderLabel("Display");
                      
                      
                        //tree = new QTreeView(this);
                             // tree->setModel(treeWidget);
                      
                        //Cpp file:
                      

                      myAction = new QAction(tr("&Add Subprogram"), treeWidget);
                      connect(myAction, SIGNAL(triggered()), this, SLOT(mySlot()));
                      myAction1 = new QAction(tr("&Delete Subprogram"), treeWidget);
                      connect(myAction1, SIGNAL(triggered()), this, SLOT(deleteSubpgm()));
                      myAction2 = new QAction(tr("&AddProgram"), treeWidget);
                      connect(myAction2, SIGNAL(triggered()), this, SLOT(AddProgram()));
                      myAction3 = new QAction(tr("&DeleteProgram"), treeWidget);
                      connect(myAction3, SIGNAL(triggered()), this, SLOT(DeleteProgram()));
                      // Then add it to your treeWidget:
                      treeWidget->addAction(myAction);
                      treeWidget->addAction(myAction1);
                      treeWidget->addAction(myAction2);
                      treeWidget->addAction(myAction3);
                      prg1 = new QTreeWidgetItem(treeWidget);
                      prg1->setIcon(0,QIcon("C:/Users/skarthick.DART0/Downloads/clapperboard.png"));
                      prg1->setText(0, tr("Program1"));
                      }

                      void Dialog::mySlot()
                      {
                      qDebug()<<"ee";
                      osloItem = new QTreeWidgetItem(prg1);
                      static int i=1;
                      osloItem->setText(0, QString("Subprogram %1").arg(i));

                      i++;
                      if(i>=12)
                      {
                          QMessageBox msgBox;
                          msgBox.setText("no more to add.");
                          msgBox.exec();
                      }
                      

                      }

                      void Dialog::deleteSubpgm()
                      {
                      osloItem = treeWidget->currentItem();
                      if(osloItem)
                      delete osloItem->parent()->takeChild(osloItem->parent()->indexOfChild(osloItem));

                      }
                      void Dialog::AddProgram()
                      {
                      prg2 = new QTreeWidgetItem(treeWidget);
                      static int j=2;
                      prg2->setIcon(0,QIcon("C:/Users/skarthick.DART0/Downloads/clapperboard.png"));
                      prg2->setText(0, QString("program %2").arg(j));
                      j++;

                      }
                      void Dialog::DeleteProgram()
                      {
                      prg2= treeWidget->currentItem();
                      if(prg2)
                      {

                      delete prg2->parent();

                      }
                      }

                      RatzzR Offline
                      RatzzR Offline
                      Ratzz
                      wrote on last edited by
                      #13

                      @karti-gesar
                      I don't see any checking for valid index . i.e index.isValid() ??

                      --Alles ist gut.

                      1 Reply Last reply
                      1
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #14

                        Hi,

                        Like @Ratzz wrote, you should use Qt::CustomContextMenu not Qt::ActionsContextMenu.

                        The use of CustomContextMenu will trigger the customContextMenuRequested signal and you'll build your contextual menu in the slot connected to it.

                        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
                        1
                        • K Offline
                          K Offline
                          karti gesar
                          wrote on last edited by
                          #15

                          i will try it,,,

                          1 Reply Last reply
                          1

                          • Login

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