简体   繁体   English

移动项目时 QTreeWidget 会弹回

[英]QTreeWidget snaps back when moving items

When I'm using a QTreeWidget (via qdesigner) with the dragDropMode set to InternalMove and then attempt to move one of the items, the scrollbar snaps back to the original position of the item I moved.当我使用 QTreeWidget(通过 qdesigner)并将 dragDropMode 设置为 InternalMove,然后尝试移动其中一个项目时,滚动条会弹回到我移动的项目的原始位置。 I would like the scrollbar to stay at the position the item was dropped.我希望滚动条停留在项目被丢弃的位置。 Is there an easy way to do that?有没有简单的方法来做到这一点? I adjusted the defaultDropAction, but to no avail.我调整了defaultDropAction,但无济于事。

Figured it out...弄清楚了...

class MyTree : public QTreeWidget
{
   Q_OBJECT
   public:
      MyTree(QWidget *parent = 0);
   signals:
      void verticalScrollBarValue(int val);
   protected slots:
      void setTreeVerticalScroll(int val);
   protected:
      void dropEvent(QDropEvent *event);
};
MyTree::MyTree(QWidget *parent) : QTreeWidget(parent)
{
   connect(this, &MyTree::verticalScrollBarValue,
           this, &MyTree::setTreeVerticalScroll);
}

void MyTree::setTreeVerticalScroll(int val)
{
   verticalScrollbar()->setValue(val);
}

void MyTree::dropEvent(QDropEvent *event)
{
   int v = verticalScrollbar()->value();
   QTreeWidget::dropEvent(event);
   emit verticalScrollBarValue(v);
}

Disclaimer: There may be a typo in there somewhere, I can't copy directly from my work environment.免责声明:某处可能有错字,我无法直接从我的工作环境中复制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM