treeWidget 支持拖拽 并改变图标样式
This commit is contained in:
parent
6735013886
commit
3c19dec3f6
5 changed files with 110 additions and 0 deletions
|
@ -58,6 +58,7 @@
|
||||||
1. - [ QListView 加按钮和 排序](QListView/)
|
1. - [ QListView 加按钮和 排序](QListView/)
|
||||||
1. - [ Json生成QTreeWidget](Json生成QTreeWidget/)
|
1. - [ Json生成QTreeWidget](Json生成QTreeWidget/)
|
||||||
1. - [ treeWidget 节点可拖拽](https://github.com/AshotS/glowing-disco)
|
1. - [ treeWidget 节点可拖拽](https://github.com/AshotS/glowing-disco)
|
||||||
|
1. - [ treeWidget 支持拖拽 并改变图标样式](12.1拖拽显示为图片/)
|
||||||
1. - [ QSqlTableModel + QTableView 数据库查询显示表格](数据库查询显示表格/)
|
1. - [ QSqlTableModel + QTableView 数据库查询显示表格](数据库查询显示表格/)
|
||||||
|
|
||||||
|
|
||||||
|
|
105
partner_625781186/12.1拖拽显示为图片/Custom_DND_image.py
Normal file
105
partner_625781186/12.1拖拽显示为图片/Custom_DND_image.py
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
#!/usr/bin/env python2
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import re
|
||||||
|
|
||||||
|
from PyQt5 import QtGui, QtCore, QtWidgets
|
||||||
|
from PyQt5.Qt import QDir
|
||||||
|
|
||||||
|
from PyQt5 import QtGui, QtWidgets, QtCore, QtWinExtras
|
||||||
|
from PyQt5.QtCore import *
|
||||||
|
from PyQt5.QtGui import *
|
||||||
|
from PyQt5.QtWidgets import *
|
||||||
|
|
||||||
|
"""
|
||||||
|
Created on $date$ <br>
|
||||||
|
description: 树视图支持拖拽 并改变图标样式 <br>
|
||||||
|
author: 东love方 <br>
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
class MyTreeWidget(QTreeWidget):
|
||||||
|
|
||||||
|
def mouseMoveEvent_xxx(self, e):
|
||||||
|
mimeData = QtCore.QMimeData()
|
||||||
|
drag = QDrag(self)
|
||||||
|
drag.setMimeData(mimeData)
|
||||||
|
|
||||||
|
# pixmap = QPixmap()
|
||||||
|
# drag.setPixmap(pixmap)
|
||||||
|
|
||||||
|
# drag.setHotSpot(e.pos())
|
||||||
|
|
||||||
|
# QTreeWidget.mouseMoveEvent(self,e)
|
||||||
|
drag.exec_(QtCore.Qt.MoveAction)
|
||||||
|
|
||||||
|
def dropEvent(self, e):
|
||||||
|
QTreeWidget.dropEvent(self, e)
|
||||||
|
self.expandAll()
|
||||||
|
e.accept()
|
||||||
|
|
||||||
|
def startDrag(self, supportedActions):
|
||||||
|
listsQModelIndex = self.selectedIndexes()
|
||||||
|
if listsQModelIndex:
|
||||||
|
dataQMimeData = self.model().mimeData(listsQModelIndex)
|
||||||
|
if not dataQMimeData:
|
||||||
|
return None
|
||||||
|
dragQDrag = QDrag(self)
|
||||||
|
dragQDrag.setPixmap(
|
||||||
|
QPixmap(QDir.currentPath() + "/if_Cursor_drag_arrow_103039.png")) # <- For put your
|
||||||
|
# custom image here
|
||||||
|
dragQDrag.setMimeData(dataQMimeData)
|
||||||
|
defaultDropAction = QtCore.Qt.IgnoreAction
|
||||||
|
if ((supportedActions & QtCore.Qt.CopyAction) and (self.dragDropMode() != QAbstractItemView.InternalMove)):
|
||||||
|
defaultDropAction = QtCore.Qt.CopyAction
|
||||||
|
dragQDrag.exec_(supportedActions, defaultDropAction)
|
||||||
|
|
||||||
|
|
||||||
|
class TheUI(QDialog):
|
||||||
|
|
||||||
|
def __init__(self, args=None, parent=None):
|
||||||
|
super(TheUI, self).__init__(parent)
|
||||||
|
self.layout = QVBoxLayout(self)
|
||||||
|
treeWidget = MyTreeWidget()
|
||||||
|
|
||||||
|
button = 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(QAbstractItemView.InternalMove)
|
||||||
|
|
||||||
|
self.resize(500, 500)
|
||||||
|
for i in range(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 = QTreeWidgetItem(parent, ['script %s' % i, '1', '150'])
|
||||||
|
return item
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app = QApplication(sys.argv)
|
||||||
|
gui = TheUI()
|
||||||
|
gui.show()
|
||||||
|
app.exec_()
|
4
partner_625781186/12.1拖拽显示为图片/README.md
Normal file
4
partner_625781186/12.1拖拽显示为图片/README.md
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
# 12.1拖拽显示为图片
|
||||||
|
|
||||||
|
|
||||||
|
![1](ScreenShot/1.gif)
|
BIN
partner_625781186/12.1拖拽显示为图片/ScreenShot/1.gif
Normal file
BIN
partner_625781186/12.1拖拽显示为图片/ScreenShot/1.gif
Normal file
Binary file not shown.
After Width: | Height: | Size: 251 KiB |
BIN
partner_625781186/12.1拖拽显示为图片/if_Cursor_drag_arrow_103039.png
Normal file
BIN
partner_625781186/12.1拖拽显示为图片/if_Cursor_drag_arrow_103039.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 383 B |
Loading…
Reference in a new issue