构造函数
this->setSelectionMode(QAbstractItemView::ExtendedSelection);//全局可多选
this->viewport()->installEventFilter(this);
事件拦截
bool ProjectTreeView::eventFilter(QObject* watched, QEvent* event)
{
if(watched == this->viewport())
{
if(event->type()==QEvent::MouseMove)//禁用多选模式下,鼠标左键点击且移动的多选功能
{
QMouseEvent * mouseEvent = static_cast<QMouseEvent * >(event);
if(mouseEvent->buttons()== Qt::LeftButton)
{
return true;
}
}
if(event->type()==QEvent::MouseButtonDblClick)//先响应单机再响应双击
{
QMouseEvent * mouseEvent = static_cast<QMouseEvent * >(event);
if(mouseEvent->button()== Qt::LeftButton)
{
this->selectionModel()->clearSelection();
}
}
else if(event->type()==QEvent::MouseButtonPress)
{
QMouseEvent * mouseEvent = static_cast<QMouseEvent * >(event);
if(mouseEvent->button()== Qt::LeftButton)
{
static bool isFirstControl = false;
if(this->selectionModel()->selectedIndexes().size() == 0)
{
isFirstControl = false;
}
if (QApplication::keyboardModifiers() == Qt::ControlModifier)
{
QModelIndex index;
index =this->indexAt(mouseEvent->pos());
ProjectItemData *pParentItem = static_cast<ProjectItemData*>(m_pProjectTreeModel->parent(index).internalPointer());
if(!pParentItem) return QWidget::eventFilter(watched,event);
if(pParentItem->getType() == ProjectItemData::XYPlots||pParentItem->getType() == ProjectItemData::DirectivityPlots
||pParentItem->getType() == ProjectItemData::MonitorPlots||pParentItem->getType() == ProjectItemData::TimeFourierTransform
||pParentItem->getType() == ProjectItemData::TimeFourierTransformGroup)
{
ProjectItemData *pItem = static_cast<ProjectItemData*>(index.internalPointer());
if(!isFirstControl)
{
this->selectionModel()->clear();
m_currentNodeType = pItem->getType();
isFirstControl = true;
}
if(m_currentNodeType==pItem->getType())
{
if(pItem->getType() == ProjectItemData::ItemType::Fourier)
{
emit signal_pressFourier(pItem);
m_isFourierSingnalSelect = false;
}
emit signal_selectionChange(pItem,index);
return QWidget::eventFilter(watched,event);
}
}
return true;
}
emit signal_clrearFouriers();
this->selectionModel()->clear();
isFirstControl = false;
m_itemList.clear();
m_indexs.clear();
}
}
}
return QWidget::eventFilter(watched,event);
}
void ProjectTreeView::slot_selectionChange(const QModelIndex& index)
{
for (int i = 0; i < m_itemList.size(); ++i)
{
if(pItem->getId() == m_itemList.at(i)->getId())
return;
}
m_itemList.append(pItem);
m_indexs.append(index);
}
当自己的选择后要做的事出发后要
this->selectionModel()->clear();
m_itemList.clear();
m_indexs.clear();
批量删除选择的节点class ProjectTreeModel : public QAbstractItemModel中
bool ProjectTreeModel::deleteNodes(const QList<QModelIndex>& listModelIndex)
{
if(listModelIndex.size()==0)
{
return false;
}
if(!listModelIndex.at(0).parent().isValid())
{
return false;
}
for (int i = 0; i < listModelIndex.size(); ++i)
{
beginRemoveRows(listModelIndex.at(i).parent(),listModelIndex.at(i).row(),listModelIndex.at(i).row());
TreeItemBaseData *pItem = static_cast<TreeItemBaseData*>(listModelIndex.at(i).internalPointer());
if(pItem)
{
TreeItemBaseData *parentNode = pItem->getParent();
if(parentNode)
{
parentNode->removeChild(pItem);
}
}
}
endRemoveRows();
return true;
}
网友评论