我有两个QListWidgets并排放置。我想设置事件启用功能
将项目拖到list2时从list1中删除
,反之亦然
代码如下:
from PyQt5.QtWidgets import QApplication, QWidget, QListWidget, QHBoxLayout,QListWidgetItem
from PyQt5.QtGui import QIcon
import sys
import glob
import os
class Window(QWidget):
def __init__(self):
super().__init__()
self.myListWidget1 = QListWidget()
self.myListWidget2 = QListWidget()
self.myListWidget1.setAcceptDrops(True)
self.myListWidget1.setDragEnabled(True)
self.myListWidget2.setAcceptDrops(True)
self.myListWidget2.setDragEnabled(True)
self.setGeometry(300, 350, 500, 300)
self.myLayout = QHBoxLayout()
self.myLayout.addWidget(self.myListWidget1)
self.myLayout.addWidget(self.myListWidget2)
os.chdir("./Downloads")
for file in glob.glob("*.pdf"):
QListWidgetItem(file, self.myListWidget1)
self.setWindowTitle('Drag and Drop');
self.setLayout(self.myLayout)
self.show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
答案 0 :(得分:1)
必须将其设置为defaultDropAction
中QListWidget
的{{3}}:
self.myListWidget1.setDefaultDropAction(Qt.MoveAction)
self.myListWidget2.setDefaultDropAction(Qt.MoveAction)