2016-06-21 72 views
0

我試圖在將鼠標移動到自定義QTreeWidget中的某個項目上但僅當放置位置爲QAbstractItemView :: OnItem時才顯示Qt :: ForbiddenCursor。 下面是代碼僅當鼠標在QTreeWidget中的OnItem時才顯示ForbiddenCursor

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時調用相同類型的項目,同時將光標懸停在這些項目上方。

只有鼠標是OnItem時,如何顯示ForbiddenCursor?

回答

0

調用QTreeWidget :: dragMoveEvent(event);在更改遊標之前解決了問題。以下是固定代碼現在的外觀:

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); 
    } 
} 
相關問題