Skip to content

Instantly share code, notes, and snippets.

@oglops
Last active September 7, 2016 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oglops/d632849c386bbbe12b2e to your computer and use it in GitHub Desktop.
Save oglops/d632849c386bbbe12b2e to your computer and use it in GitHub Desktop.
simple tree widget transparent dragging by customizing 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(self, e):
listsQModelIndex = self.selectedIndexes()
if listsQModelIndex:
dataQMimeData = self.model().mimeData(listsQModelIndex)
index = self.indexFromItem(self.currentItem(), 0)
row = index.row()
drag_rect = self.visualItemRect(self.itemFromIndex(index))
header_height = self.header().sizeHint().height()
row_height = self.rowHeight(index)
# screenshot_y = row * row_height + header_height +2
screenshot_y = drag_rect.y() + row_height
pixmap = QtGui.QPixmap.grabWidget(self, 0, screenshot_y, -1, row_height)
painter = QtGui.QPainter(pixmap)
painter.setCompositionMode(painter.CompositionMode_DestinationIn)
painter.fillRect(pixmap.rect(), QtGui.QColor(0, 0, 0, 127))
painter.end()
# make a QDrag
drag = QtGui.QDrag(self)
# put our MimeData
drag.setMimeData(dataQMimeData)
# set its Pixmap
drag.setPixmap(pixmap)
# shift the Pixmap so that it coincides with the cursor position
# drag.setHotSpot(e.pos())
drag.setHotSpot(QtCore.QPoint(e.pos().x(), e.pos().y() - screenshot_y + row_height))
# start the drag operation
# exec_ will return the accepted action from dropEvent
drag.exec_()
class TheUI(QtGui.QDialog):
def __init__(self, args=None, parent=None):
super(TheUI, self).__init__(parent)
self.layout1 = QtGui.QVBoxLayout(self)
treeWidget = MyTreeWidget()
# treeWidget.setSelectionMode( QtGui.QAbstractItemView.ExtendedSelection )
button1 = QtGui.QPushButton('Add')
button2 = QtGui.QPushButton('Add Child')
self.layout1.addWidget(treeWidget)
self.layout2 = QtGui.QHBoxLayout()
self.layout2.addWidget(button1)
self.layout2.addWidget(button2)
self.layout1.addLayout(self.layout2)
treeWidget.setHeaderHidden(True)
self.treeWidget = treeWidget
self.button1 = button1
self.button2 = button2
self.button1.clicked.connect(lambda *x: self.addCmd())
self.button2.clicked.connect(lambda *x: self.addChildCmd())
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.addChildCmd()
if i == 4:
self.addCmd('%s-2' % i, parent=item)
self.treeWidget.expandAll()
self.setStyleSheet("QTreeWidget::item{ height: 30px; }")
def addChildCmd(self):
parent = self.treeWidget.currentItem()
self.addCmd(parent=parent)
self.treeWidget.setCurrentItem(parent)
def addCmd(self, i=None, parent=None):
'add a level to tree widget'
root = self.treeWidget.invisibleRootItem()
if not parent:
parent = root
if i is None:
if parent == root:
i = self.treeWidget.topLevelItemCount()
else:
i = str(parent.text(0))[7:]
i = '%s-%s' % (i, parent.childCount() + 1)
item = QtGui.QTreeWidgetItem(parent, ['script %s' % i, '1', '150'])
self.treeWidget.setCurrentItem(item)
self.treeWidget.expandAll()
return item
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
gui = TheUI()
gui.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment