本文是对《Python Qt GUI快速编程》的第10章的例子拖放用Python3+PyQt5进行改写,对图表列表,表格等进行相互拖放,基本原理雷同,均采用setAcceptDrops(True)和setDragEnabled(True)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#!/usr/bin/env python3 import os import sys from PyQt5.QtCore import Qt from PyQt5.QtWidgets import (QApplication, QDialog, QHBoxLayout, QListWidget, QListWidgetItem, QSplitter, QTableWidget) from PyQt5.QtGui import QIcon class Form(QDialog): def __init__( self , parent = None ): super (Form, self ).__init__(parent) listWidget = QListWidget() listWidget.setAcceptDrops( True ) listWidget.setDragEnabled( True ) path = os.path.dirname(__file__) for image in sorted (os.listdir(os.path.join(path, "images" ))): if image.endswith( ".png" ): item = QListWidgetItem(image.split( "." )[ 0 ].capitalize()) item.setIcon(QIcon(os.path.join(path, "images/{0}" . format (image)))) listWidget.addItem(item) iconListWidget = QListWidget() iconListWidget.setAcceptDrops( True ) iconListWidget.setDragEnabled( True ) iconListWidget.setViewMode(QListWidget.IconMode) tableWidget = QTableWidget() tableWidget.setRowCount( 5 ) tableWidget.setColumnCount( 2 ) tableWidget.setHorizontalHeaderLabels([ "Column #1" , "Column #2" ]) tableWidget.setAcceptDrops( True ) tableWidget.setDragEnabled( True ) splitter = QSplitter(Qt.Horizontal) splitter.addWidget(listWidget) splitter.addWidget(iconListWidget) splitter.addWidget(tableWidget) layout = QHBoxLayout() layout.addWidget(splitter) self .setLayout(layout) self .setWindowTitle( "Drag and Drop" ) if __name__ = = "__main__" : app = QApplication(sys.argv) form = Form() form.show() app.exec_() |
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xiaoyangyang20/article/details/54706612