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

时间:2014-02-22 18:28:14

标签: python qt4 pyqt4 pyside

我的问题是我有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()

1 个答案:

答案 0 :(得分:4)

我将keyPressEvent添加到List类,在这种情况下,我检查了按下的键是否为del键,如果是,则称为_del_item方法。我还向addItems添加了mainWindow方法,以便向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()