C++ 如何获得QTreeWidget内部项目移动的通知?

C++ 如何获得QTreeWidget内部项目移动的通知?,c++,qt,notifications,qtreewidget,C++,Qt,Notifications,Qtreewidget,我将一个QTreeWidget子类化,将其dragDropMode设置为InternalMove,并用自定义项填充它,其中一些可以拖动,其他可以接受拖放。用户可以按预期在树中移动项目。但我需要被通知项目顺序的变化,并做出适当的反应。不幸的是,我无法连接到与树内项目移动相关的信号 我尝试获取QTreeWidget的底层模型()的句柄,然后连接到它的rowsMoved信号,但它似乎在内部移动过程中没有发出 我重新实现了QTreeWidget的dropEvent(),但无法确定那里的目标行索引 显然,

我将一个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()和关联的ItemUpper()、ItemDownlow()混乱,或者在不同父级之间移动项目时,至少有助于补充它:

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);
}
void MyTreeWidget::dropEvent(QDropEvent*事件)
{
//获取要拖动的项目的列表
QList dragItems=selectedItems();
//在拖动之前找出它们的行号
QList fromRows;
QTreeWidgetItem*项目;
foreach(item,dragItems)fromRows.append(indexFromItem(item.row());
//默认实现负责树内的实际移动
QTreeWidget::dropEvent(事件);
//再次查询拖动项的索引
QList-toRows;
foreach(item,dragItems)toRows.append(indexFromItem(item.row());
//以某种有用的方式通知订阅者
发射项移动(从行、旋转);
}

OP实际上询问了如何获得有关内部移动的通知,即如何在不细分QTreeWidget的情况下进行通知(至少这是我使用内部移动的方式,因为它是内置功能)。我刚刚找到了一种方法:连接到QTreeWidget模型的信号


不幸的是,恢复的索引指向鼠标光标下的项目,而不是由放置指示器指示的行。将其向上移动一点,您可以看到上面的项目,向下移动,您可以看到下面的项目,同时下降指示器保持不变:(@neuviemeporte:对于我自己的用例来说,这似乎不是一个问题,但可能这就是
dropIndicatorPosition()
的作用。完美。我没有注意到这一点。谢谢!这是否也适用于移动,而不仅仅是复制?
connect(treeWidget->model(), SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(rowsInserted(const QModelIndex &, int, int)));