Adding contextmenu for QTreeWidgetItem .



  • 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 .



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



  • 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();
    @



  • 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);
    }@



  • 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);
    }@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.