qt重写QTreeWidget实现拖动item到树的顶部的时候,树滚轮上移,拖动item到底部的时候,树滚轮下移

 主要是在重写的QTreeWidget类中的dragMoveEvent()函数中添加如下代码:

void CusTreeWidget::dragMoveEvent(QDragMoveEvent *event)
{
    QRect rectBottom = QRect(0,this->height()-50,300,50);
    QRect rectTop = QRect(0,0,300,50);
    int delta = 0;
    if(oldPt!=event->pos())
    {
        delta = 1;
    }
    if(rectBottom.contains(event->pos()))
    {
        int nBarValue = this->verticalScrollBar()->value();
        this->verticalScrollBar()->setValue(nBarValue+delta);
    }
    if(rectTop.contains(event->pos()))
    {
        int nBarValue = this->verticalScrollBar()->value();
        this->verticalScrollBar()->setValue(nBarValue-delta);
    }
    
    oldPt=event->pos();
    event->accept();

}

源码下载链接:https://download.csdn.net/download/weixin_43935474/87663546

你可能感兴趣的:(QT,qt)