一、双击编辑
1、定义连接
connect(m_treeWidget, &QTreeView::doubleClicked, this, &FileViewPanelCreator::onItemDoubleClicked); connect(m_treeWidget->itemDelegate(), &QAbstractItemDelegate::closeEditor, this, &FileViewPanelCreator::onCloseEditor);
2、实现函数
// 槽函数,处理双击事件 void FileViewPanelCreator::onItemDoubleClicked(const QModelIndex &index) { if (index.isValid()) { QTreeWidgetItem* root = m_treeWidget->topLevelItem(0); QTreeWidgetItem* item = root->child(index.row()); m_treeWidget->openPersistentEditor(item); } } // 槽函数,处理编辑完成事件 void FileViewPanelCreator::onCloseEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint) { Q_UNUSED(hint); // 在此处理编辑完成后的操作 QModelIndex editedIndex = m_treeWidget->currentIndex(); QTreeWidgetItem* root = m_treeWidget->topLevelItem(0); QTreeWidgetItem* item = root->child(editedIndex.row()); m_treeWidget->closePersistentEditor(item); }
二、鼠标右键菜单
1、定义连接
m_treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);//必须添加 connect(m_treeWidget, &QTreeWidget::customContextMenuRequested, this, &FileViewPanelCreator::onTreeWidgetCustomContextMenuRequested);
2、实现函数
void FileViewPanelCreator::onTreeWidgetCustomContextMenuRequested(const QPoint &pos) { QTreeWidgetItem* curItem = m_treeWidget->itemAt(pos); if(curItem != nullptr) { QMenu *popMenu = new QMenu(m_treeWidget); QAction *actionNew = new QAction("Attribute",m_treeWidget); connect(actionNew, &QAction::triggered, this, &FileViewPanelCreator::ActionNew_Attribute); popMenu->addAction(actionNew); popMenu->exec(QCursor::pos()); delete popMenu; delete actionNew; } }
标签:FileViewPanelCreator,QTreeWidget,QTreeWidgetItem,connect,treeWidget,popMenu,acti From: https://www.cnblogs.com/wxzhrj/p/17945773