gpt4 book ai didi

python - Pyside PyQt,如何将 Key_Delete 连接到 4 个不同的 QListWidget

转载 作者:太空狗 更新时间:2023-10-30 03:02:25 24 4
gpt4 key购买 nike

我的问题是我有 4 个 QListWidget,我希望能够使用 del 键删除每个列表中的选定项目。因此,当我将 del 键连接到一个列表时,它工作正常,但一旦我将它连接到第二个或更多,它就不再工作了。到目前为止,当您只处理一个 QListWidget 时,我只找到示例和解释。有谁知道我在这里做错了什么?

import sys, os
from PySide import QtGui, QtCore

# List item to drag and drop files from windows explorer
class List(QtGui.QListWidget):
def __init__(self, parent):
super(List, self).__init__(parent)

self.setAcceptDrops(True)
self.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)

def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()

def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore()

def dropEvent(self, event):
all_items = []
for i in xrange(self.count()):
all_items.append(self.item(i))
url = event.mimeData().urls()
if all_items == []:
for strUrl in url:
self.addItem(strUrl.path()[1:])
else:
for strUrl in url:
if any(item.text() in strUrl.path()[1:] for item in all_items):
continue
else:
self.addItem(strUrl.path()[1:])

def _del_item(self):
for item in self.selectedItems():
self.takeItem(self.row(item))


class MainWindow(QtGui.QWidget):

def __init__(self):
super(MainWindow, self).__init__()
self.init_ui()

def init_ui(self):
folder_list = [' ', 'Concept Art', 'Scripts', 'Storyboard', 'Directors Input', 'Look Dev', 'Corrections',
'References', 'Still References', 'Video References', 'Sound', 'Sound References']
self.box_one = QtGui.QComboBox(parent=self)
self.box_two = QtGui.QComboBox(parent=self)
self.box_three = QtGui.QComboBox(parent=self)
self.box_four = QtGui.QComboBox(parent=self)
for i in folder_list:
self.box_one.addItems([i])
self.box_two.addItems([i])
self.box_three.addItems([i])
self.box_four.addItems([i])
self.list_one = List(self)
self.list_one.setGeometry(0, 0, 500, 100)
self.list_two = List(self)
self.list_two.setGeometry(0, 0, 500, 100)
self.list_three = List(self)
self.list_three.setGeometry(0, 0, 500, 100)
self.list_four = List(self)
self.list_four.setGeometry(0, 0, 500, 100)

# connect del key to lists
del_one = QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Delete), self.list_one)
del_two = QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Delete), self.list_two)
del_three = QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Delete), self.list_three)
del_four = QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Delete), self.list_four)
self.connect(del_one, QtCore.SIGNAL('activated()'), self.list_one._del_item)
self.connect(del_two, QtCore.SIGNAL('activated()'), self.list_two._del_item)
self.connect(del_three, QtCore.SIGNAL('activated()'), self.list_three._del_item)
self.connect(del_four, QtCore.SIGNAL('activated()'), self.list_four._del_item)

v_box = QtGui.QVBoxLayout()
v_box.setSpacing(10)
v_box.addWidget(self.box_one)
v_box.addWidget(self.list_one)
v_box.addWidget(self.box_two)
v_box.addWidget(self.list_two)
v_box.addWidget(self.box_three)
v_box.addWidget(self.list_three)
v_box.addWidget(self.box_four)
v_box.addWidget(self.list_four)

self.setLayout(v_box)
self.setGeometry(100, 100, 500, 190)
self.setWindowTitle('Arx Submitter')
self.show()

def main():

app = QtGui.QApplication(sys.argv)
main_win = MainWindow()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

最佳答案

我将 keyPressEvent 添加到 List 类,在这个事件中我检查按下的键是否是 del 键,如果是,调用 _del_item 方法。我还向 mainWindow 添加了 addItems 方法,以将一些项目添加到 listWidgets。这很好用。我为 del key 注释掉了你的连接。

只需复制并粘贴此代码,运行它并尝试删除项目;)

import sys, os
from PySide import QtGui, QtCore
from PySide.QtCore import Qt

# List item to drag and drop files from windows explorer
class List(QtGui.QListWidget):
def __init__(self, parent):
super(List, self).__init__(parent)

self.setAcceptDrops(True)
self.setSelectionMode(QtGui.QAbstractItemView.MultiSelection)

def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()

def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore()

def dropEvent(self, event):
all_items = []
for i in xrange(self.count()):
all_items.append(self.item(i))
url = event.mimeData().urls()
if all_items == []:
for strUrl in url:
self.addItem(strUrl.path()[1:])
else:
for strUrl in url:
if any(item.text() in strUrl.path()[1:] for item in all_items):
continue
else:
self.addItem(strUrl.path()[1:])

def keyPressEvent(self, event):
if event.key() == Qt.Key_Delete:
self._del_item()

def _del_item(self):
for item in self.selectedItems():
self.takeItem(self.row(item))


class MainWindow(QtGui.QWidget):

def __init__(self):
super(MainWindow, self).__init__()
self.init_ui()

def init_ui(self):
folder_list = [' ', 'Concept Art', 'Scripts', 'Storyboard', 'Directors Input', 'Look Dev', 'Corrections',
'References', 'Still References', 'Video References', 'Sound', 'Sound References']
self.box_one = QtGui.QComboBox(parent=self)
self.box_two = QtGui.QComboBox(parent=self)
self.box_three = QtGui.QComboBox(parent=self)
self.box_four = QtGui.QComboBox(parent=self)
for i in folder_list:
self.box_one.addItems([i])
self.box_two.addItems([i])
self.box_three.addItems([i])
self.box_four.addItems([i])
self.list_one = List(self)
self.addItems(self.list_one)
self.list_one.setGeometry(0, 0, 500, 100)
self.list_two = List(self)
self.addItems(self.list_two)
self.list_two.setGeometry(0, 0, 500, 100)
self.list_three = List(self)
self.addItems(self.list_three)
self.list_three.setGeometry(0, 0, 500, 100)
self.list_four = List(self)
self.addItems(self.list_four)
self.list_four.setGeometry(0, 0, 500, 100)

# connect del key to lists
#del_one = QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Delete), self.list_one)
#del_two = QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Delete), self.list_two)
#del_three = QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Delete), self.list_three)
#del_four = QtGui.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Delete), self.list_four)
#self.connect(del_one, QtCore.SIGNAL('activated()'), self.list_one._del_item)
#self.connect(del_two, QtCore.SIGNAL('activated()'), self.list_two._del_item)
#self.connect(del_three, QtCore.SIGNAL('activated()'), self.list_three._del_item)
#self.connect(del_four, QtCore.SIGNAL('activated()'), self.list_four._del_item)

v_box = QtGui.QVBoxLayout()
v_box.setSpacing(10)
v_box.addWidget(self.box_one)
v_box.addWidget(self.list_one)
v_box.addWidget(self.box_two)
v_box.addWidget(self.list_two)
v_box.addWidget(self.box_three)
v_box.addWidget(self.list_three)
v_box.addWidget(self.box_four)
v_box.addWidget(self.list_four)

self.setLayout(v_box)
self.setGeometry(100, 100, 500, 190)
self.setWindowTitle('Arx Submitter')
self.show()

def addItems(self, widget):
for i in range(10):
widget.addItem("Item "+ str(i))

def main():

app = QtGui.QApplication(sys.argv)
main_win = MainWindow()
sys.exit(app.exec_())

if __name__ == '__main__':
main()

关于python - Pyside PyQt,如何将 Key_Delete 连接到 4 个不同的 QListWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21958535/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com