[英]How can I get notified of internal item moves inside a QTreeWidget?
我將一個QTreeWidget子類化,將其dragDropMode設置為InternalMove並使用自定義項填充它,其中一些可以被拖動,其他可以接受丟棄。 用戶可以按預期在樹周圍移動項目。 但我需要通知項目順序的變化並做出適當的反應。 不幸的是,沒有與我可以連接的樹內物品移動相關的信號。
我嘗試獲取QTreeWidget的底層模型()的句柄,然后連接到其rowsMoved信號,但在內部移動期間似乎沒有發出。
我重新實現了QTreeWidget的dropEvent() ,但沒有辦法確定那里的目標行索引。
顯然,對於內部移動,不會調用dropMimeData()事件。
我可以嘗試其他任何方法嗎? 謝謝。
在重新實現的dropEvent()
,您應該能夠找到目標行索引和項目:
void
subclass::dropEvent(QDropEvent* event)
{
QModelIndex index = indexAt(event->pos());
if (!index.isValid()) { // just in case
event->setDropAction(Qt::IgnoreAction);
return;
}
QTreeWidgetItem* destination_item = itemFromIndex(index);
....
}
順便說一句,我想出了另一種方法來找出哪個元素准確移動到哪里,哪個回避整個dropIndicatorPosition()和相關的itemAbove(),itemBelow()混亂,或者至少在某些方面有所幫助,在不同時間之間移動項目時父母:
void MyTreeWidget::dropEvent(QDropEvent *event)
{
// get the list of the items that are about to be dragged
QList<QTreeWidgetItem*> dragItems = selectedItems();
// find out their row numbers before the drag
QList<int> fromRows;
QTreeWidgetItem *item;
foreach(item, dragItems) fromRows.append(indexFromItem(item).row());
// the default implementation takes care of the actual move inside the tree
QTreeWidget::dropEvent(event);
// query the indices of the dragged items again
QList<int> toRows;
foreach(item, dragItems) toRows.append(indexFromItem(item).row());
// notify subscribers in some useful way
emit itemsMoved(fromRows, toRows);
}
OP實際上詢問了如何獲得有關內部移動的通知,即如何在不對QTreeWidget進行子類化的情況下執行此操作(至少這是我使用內部移動的方式,因為它是內置的功能)。 我剛剛找到了一種方法:連接到QTreeWidget模型的rowsInserted()信號!
connect(treeWidget->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.