Skip to main content
 首页 » 编程设计

python之在QTreeWidget中拖动时如何在释放之前隐藏屏幕截图

2024年10月25日10soundcode

我想在 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

请您参考如下方法:

非常有趣!

我在 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