【问题标题】:How to hide the screenshot before releasing when dragging in QTreeWidget?QTreeWidget中拖动时如何在释放前隐藏截图?
【发布时间】:2014-08-21 15:20:46
【问题描述】:

我想隐藏“拖动指示器”(拖动行截图)在QTreeWidget中拖动时只显示鼠标箭头,可以吗?

我试图重新实现mouseMoveEvent,“拖动指示器”消失了,但同时,拖放被禁用,我错过了什么

代码:

#!/usr/bin/env python2
import os
import sys
import re

from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import Qt, QString


class MyTreeWidget(QtGui.QTreeWidget):

    def mouseMoveEvent_xxx(self, e):
        mimeData = QtCore.QMimeData()
        drag = QtGui.QDrag(self)
        drag.setMimeData(mimeData)

        # pixmap = QtGui.QPixmap()
        # drag.setPixmap(pixmap)

        # drag.setHotSpot(e.pos())

        # QtGui.QTreeWidget.mouseMoveEvent(self,e)
        drag.exec_(QtCore.Qt.MoveAction)


    def dropEvent(self,e):
        QtGui.QTreeWidget.dropEvent(self,e)
        self.expandAll()
        e.accept()



class TheUI(QtGui.QDialog):

    def __init__(self, args=None, parent=None):
        super(TheUI, self).__init__(parent)
        self.layout = QtGui.QVBoxLayout(self)
        treeWidget = MyTreeWidget()

        button = QtGui.QPushButton('Add')
        self.layout.addWidget(treeWidget)
        self.layout.addWidget(button)
        treeWidget.setHeaderHidden(True)

        self.treeWidget = treeWidget
        self.button = button
        self.button.clicked.connect(lambda *x: self.addCmd())

        HEADERS = ( "script", "chunksize", "mem" )
        self.treeWidget.setHeaderLabels(HEADERS)
        self.treeWidget.setColumnCount( len(HEADERS) )

        self.treeWidget.setColumnWidth(0,160)
        self.treeWidget.header().show()

        self.treeWidget.setDragDropMode(QtGui.QAbstractItemView.InternalMove)

        self.resize(500,500)
        for i in xrange(6):
            item =self.addCmd(i)
            if i in (3,4):
                self.addCmd('%s-1' % i,parent=item)

        self.treeWidget.expandAll()
        self.setStyleSheet("QTreeWidget::item{ height: 30px;  }")

    def addCmd(self, i,parent=None):
        'add a level to tree widget'

        root = self.treeWidget.invisibleRootItem()
        if not parent:
            parent=root
        item = QtGui.QTreeWidgetItem(parent,['script %s' %i,'1','150'])
        return item

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    gui = TheUI()
    gui.show()
    app.exec_()

截图:

我想隐藏蓝色拖动条,同时保留鼠标箭头。(或者给它一些自定义像素图,或者更好地在拖动条周围画一个虚线轮廓),这个想法是不想掩盖它后面的树内容,(在我的系统中合成管理器被禁用,所以半透明是不可能的)

如果你将 mouseMoveEvent_xxx 重命名为 mouseMoveEvent,你会明白我的意思,拖动被禁用,虽然我已经将 dragDropMode 设置为 InternalMove

【问题讨论】:

    标签: python drag-and-drop delegates pyqt qtreewidget


    【解决方案1】:

    非常有趣!

    我在 Qt 4 (C++) 中找到了拖动指示器的行为。最后,我发现void QAbstractItemView::startDrag(Qt::DropActions supportedActions)方法是控制开始拖动和拖动指示器。在Line 3217 - 3238 可以判断拖动行为,第 3229 行是您想要的(设置新图像或不设置)。好的,解决方案是找到任何方法将第 3229 行删除或替换新图像。绝对,我们必须重写这个方法。但是,新问题是 C++ 和你在 python 中的使用。我们必须改变一些东西才能把它放在 python 中可以工作。 (硬核……)

    好的,就是这样。覆盖方法QAbstractItemView.startDrag (self, Qt.DropActions supportedActions);

    来自 C++;

        .
        .
        .
    void QAbstractItemView::startDrag(Qt::DropActions supportedActions)
    {
    Q_D(QAbstractItemView);
    QModelIndexList indexes = d->selectedDraggableIndexes();
    if (indexes.count() > 0) {
        QMimeData *data = d->model->mimeData(indexes);
        if (!data)
            return;
        QRect rect;
        QPixmap pixmap = d->renderToPixmap(indexes, &rect);
        rect.adjust(horizontalOffset(), verticalOffset(), 0, 0);
        QDrag *drag = new QDrag(this);
        drag->setPixmap(pixmap);
        drag->setMimeData(data);
        drag->setHotSpot(d->pressedPosition - rect.topLeft());
        Qt::DropAction defaultDropAction = Qt::IgnoreAction;
        if (supportedActions & Qt::CopyAction && dragDropMode() != QAbstractItemView::InternalMove)
            defaultDropAction = Qt::CopyAction;
        if (drag->exec(supportedActions, defaultDropAction) == Qt::MoveAction)
            d->clearOrRemove();
        }
    }
        .
        .
        .
    

    到python;

    class QCustomTreeWidget (QtGui.QTreeWidget):
        .
        .
        .
        def startDrag (self, supportedActions):
            listsQModelIndex = self.selectedIndexes()
            if listsQModelIndex:
                dataQMimeData = self.model().mimeData(listsQModelIndex)
                if not dataQMimeData:
                    return None
                dragQDrag = QtGui.QDrag(self)
                # dragQDrag.setPixmap(QtGui.QPixmap('test.jpg')) # <- For put your custom image here
                dragQDrag.setMimeData(dataQMimeData)
                defaultDropAction = QtCore.Qt.IgnoreAction
                if ((supportedActions & QtCore.Qt.CopyAction) and (self.dragDropMode() != QtGui.QAbstractItemView.InternalMove)):
                    defaultDropAction = QtCore.Qt.CopyAction;
                dragQDrag.exec_(supportedActions, defaultDropAction)
    

    实验结果

    我测试了这段代码并实现了代码,工作正常。但是,我无法在拖动指示器期间捕获图片。 (我使用的是 Windows 7)


    有用的参考:QDrag Class Reference,QAbstractItemView


    【讨论】:

      猜你喜欢
      • 2017-05-13
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 2021-08-07
      相关资源
      最近更新 更多