仅当鼠标位于 QTreeWidget 中的项目上时才显示禁止的光标
Show ForbiddenCursor only when mouse is OnItem in QTreeWidget
我试图在将鼠标移到自定义 QTreeWidget 中的某个项目上时显示 Qt::ForbiddenCursor,但仅当放置位置为 QAbstractItemView::OnItem 时。
这是代码
void XProjectTreeWidget::dragMoveEvent(QDragMoveEvent * event)
{
QTreeWidgetItem* pItem = itemAt(event->pos());
if (pItem == nullptr)
{
return;
}
XTreeItem* dropItem = dynamic_cast<XTreeItem*>(pItem);
if (dropItem == nullptr)
{
return;
}
XTreeItem::DropPosition drop;
if (!getDropPosition(drop))
{
return;
}
auto items = selectedItems();
if (items.count() == 0)
{
return;
}
auto dragItem = (XTreeItem*)items.first();
if (!dragItem->checkMoveItemPossible(dropItem, drop))
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ForbiddenCursor));
event->setDropAction(Qt::IgnoreAction);
}
else
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
event->setDropAction(Qt::MoveAction);
}
QTreeWidget::dragMoveEvent(event);
}
bool XProjectTreeWidget::getDropPosition(XTreeItem::DropPosition& drop)
{
DropIndicatorPosition dropIndicator = dropIndicatorPosition();
switch (dropIndicator)
{
case QAbstractItemView::AboveItem: drop = XTreeItem::Above; break;
case QAbstractItemView::BelowItem: drop = XTreeItem::Below; break;
case QAbstractItemView::OnItem: drop = XTreeItem::Inside; break;
default: return false;
}
return true;
}
问题是忽略操作似乎适用于 AboveItem 和 BelowItem 具有相同类型的所有项目。
dragMoveEvent 当 DropPosition 为 OnItem 时,对于相同类型的项目,同时将光标悬停在这些项目上方时,调用 dragMoveEvent。
如何仅在鼠标处于 OnItem 时显示 ForbiddenCursor?
调用QTreeWidget::dragMoveEvent(事件);在更改光标之前解决了这个问题。这是固定代码现在的样子:
void XProjectTreeWidget::dragMoveEvent(QDragMoveEvent * event)
{
// moved this call from the end of the method
QTreeWidget::dragMoveEvent(event);
QTreeWidgetItem* pItem = itemAt(event->pos());
if (pItem == nullptr)
{
return;
}
XTreeItem* dropItem = dynamic_cast<XTreeItem*>(pItem);
if (dropItem == nullptr)
{
return;
}
XTreeItem::DropPosition drop;
if (!getDropPosition(drop))
{
return;
}
auto items = selectedItems();
if (items.count() == 0)
{
return;
}
auto dragItem = (XTreeItem*)items.first();
if (!dragItem->checkMoveItemPossible(dropItem, drop))
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ForbiddenCursor));
event->setDropAction(Qt::IgnoreAction);
}
else
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
event->setDropAction(Qt::MoveAction);
}
}
我试图在将鼠标移到自定义 QTreeWidget 中的某个项目上时显示 Qt::ForbiddenCursor,但仅当放置位置为 QAbstractItemView::OnItem 时。 这是代码
void XProjectTreeWidget::dragMoveEvent(QDragMoveEvent * event)
{
QTreeWidgetItem* pItem = itemAt(event->pos());
if (pItem == nullptr)
{
return;
}
XTreeItem* dropItem = dynamic_cast<XTreeItem*>(pItem);
if (dropItem == nullptr)
{
return;
}
XTreeItem::DropPosition drop;
if (!getDropPosition(drop))
{
return;
}
auto items = selectedItems();
if (items.count() == 0)
{
return;
}
auto dragItem = (XTreeItem*)items.first();
if (!dragItem->checkMoveItemPossible(dropItem, drop))
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ForbiddenCursor));
event->setDropAction(Qt::IgnoreAction);
}
else
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
event->setDropAction(Qt::MoveAction);
}
QTreeWidget::dragMoveEvent(event);
}
bool XProjectTreeWidget::getDropPosition(XTreeItem::DropPosition& drop)
{
DropIndicatorPosition dropIndicator = dropIndicatorPosition();
switch (dropIndicator)
{
case QAbstractItemView::AboveItem: drop = XTreeItem::Above; break;
case QAbstractItemView::BelowItem: drop = XTreeItem::Below; break;
case QAbstractItemView::OnItem: drop = XTreeItem::Inside; break;
default: return false;
}
return true;
}
问题是忽略操作似乎适用于 AboveItem 和 BelowItem 具有相同类型的所有项目。 dragMoveEvent 当 DropPosition 为 OnItem 时,对于相同类型的项目,同时将光标悬停在这些项目上方时,调用 dragMoveEvent。
如何仅在鼠标处于 OnItem 时显示 ForbiddenCursor?
调用QTreeWidget::dragMoveEvent(事件);在更改光标之前解决了这个问题。这是固定代码现在的样子:
void XProjectTreeWidget::dragMoveEvent(QDragMoveEvent * event)
{
// moved this call from the end of the method
QTreeWidget::dragMoveEvent(event);
QTreeWidgetItem* pItem = itemAt(event->pos());
if (pItem == nullptr)
{
return;
}
XTreeItem* dropItem = dynamic_cast<XTreeItem*>(pItem);
if (dropItem == nullptr)
{
return;
}
XTreeItem::DropPosition drop;
if (!getDropPosition(drop))
{
return;
}
auto items = selectedItems();
if (items.count() == 0)
{
return;
}
auto dragItem = (XTreeItem*)items.first();
if (!dragItem->checkMoveItemPossible(dropItem, drop))
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ForbiddenCursor));
event->setDropAction(Qt::IgnoreAction);
}
else
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
event->setDropAction(Qt::MoveAction);
}
}