I want to right click the particular treewidgetitem..
-
treeWidget = new QTreeWidget(this);
treeWidget->setContextMenuPolicy(Qt::ActionsContextMenu);
treeWidget->setHeaderLabel("Display");Above code is what i used....for entire treewidget the right click appears...for me only for item right click should appear.....do you have any idea....
-
use
setContextMenuPolicy(Qt::CustomContextMenu)
and connectcustomContextMenuRequested(QPoint)
to your slot and accept only if index is valid. -
k i will try
-
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 }
}
-
above code is what i used...still right click appear all area of the treewidget
-
@karti-gesar
something like thisQModelIndex index = ui->treeWidget->indexAt(point); if(index .isValid()) { /// add actions here }
-
i don't know how to use this
-
@karti-gesar
This works for me
//consui->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())); }
-
ya it works ...but all the treewidget area getting right click
-
@karti-gesar said:
but all the treewidget area getting right click
What do you mean?
Show me your code
-
for particular item only i want right....but for entire widget it appears
-
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();
}
} -
@karti-gesar
I don't see any checking for valid index . i.eindex.isValid()
?? -
Hi,
Like @Ratzz wrote, you should use
Qt::CustomContextMenu
notQt::ActionsContextMenu
.The use of
CustomContextMenu
will trigger thecustomContextMenuRequested
signal and you'll build your contextual menu in the slot connected to it. -
i will try it,,,