How to find an element selected from customContextMenuRequested () in a QTreeView element?

I implemented context menus in QTreeView elements with the following code

MyDerivedQTreeView->setModel(MyDerivedQAbstractItemModel); MyDerivedQTreeView->setContextMenuPolicy(Qt::CustomContextMenu); connect(MyDerivedQTreeView, SIGNAL(customContextMenuRequested(const QPoint &)), MyDerivedQAbstractItemModel(), SLOT(contextualMenu(const QPoint &))); void MyDerivedQAbstractItemModel::contextualMenu(const QPoint& point) { QMenu *menu = new QMenu; menu->addAction(QString("Test Item"), this, SLOT(test_slot())); menu->exec(MyDerivedQTreeView->mapToGlobal(point)); } 

MyDerivedQAbstractItemModel :: contextualMenu () is called, and I see a context menu.

The problem is that the context menu should only be displayed if the user right-clicks on the element and it should be configured in accordance with the selected element.

How can I choose whether / which item will be selected from QPoint info? I'm on Qt 4.5.3.

+4
source share
3 answers

Perhaps you could use the QTreeView indexAt () method to get the clicked item before creating a custom menu.

+13
source

Perhaps this code will help you:

==> dialog.h <==

 QStandardItemModel *model; QSortFilterProxyModel *proxyModel; QTreeView *treeView; 

==> dialog.cpp <==

 void CImportTabWidget::createGUI() { ... proxyModel = new QSortFilterProxyModel; proxyModel->setDynamicSortFilter(true); treeView = new QTreeView; treeView->setEditTriggers(QAbstractItemView::NoEditTriggers); treeView->setRootIsDecorated(false); treeView->setAlternatingRowColors(true); treeView->setModel(proxyModel); model = new QStandardItemModel(0, 4); model->setHeaderData(0, Qt::Horizontal, tr("Name")); model->setHeaderData(1, Qt::Horizontal, tr("Comment")); model->setHeaderData(2, Qt::Horizontal, tr("Size")); model->setHeaderData(3, Qt::Horizontal, tr("Date")); fillTreeViewData(); proxyModel->setSourceModel(model); ... } ////////////////////////////////////////////////////////////////////////// void CImportTabWidget::createMenus() { treeView->setContextMenuPolicy(Qt::CustomContextMenu); connect(treeView, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenu(const QPoint &))); } ////////////////////////////////////////////////////////////////////////// void CImportTabWidget::contextMenu(const QPoint &widgetXY) { Q_UNUSED(widgetXY); QMenu menu(this); /*    */ deleteAct->setEnabled((!model->rowCount()) ? false : true ); deleteAllAct->setEnabled((!model->rowCount()) ? false : true ); /*   */ QModelIndex index = treeView->currentIndex(); QString fileName = model->data(model->index(index.row(), 0)).toString(); if (!fileName.isEmpty()) { importAct->setText(tr("Import %1").arg(fileName)); //deleteAct->setText(tr("Delete %1").arg(fileName)); } /*   */ menu.addAction(deleteAct); menu.addAction(deleteAllAct); menu.exec(QCursor::pos()); } 

Good luck

+2
source

The QTreeWidget :: currentItem () function returns the item by right-clicking. It is not clear that this is done based on the description, but based on my use, this is what it does.

0
source

Source: https://habr.com/ru/post/1307329/


All Articles