2014-02-22 53 views
4

我的問題是,我有4 QListWidget,我想能夠刪除每個列表中與del鍵的選定項目。 所以,當我只連接del鍵到一個列表,它工作正常,但只要我連接到第二個或更多它不再工作。到目前爲止,我只在處理僅一個QListWidget時才找到示例和解釋。 有誰知道我在這裏做錯了嗎?Pyside PyQt,如何將Key_Delete連接到4個不同的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() 

回答

4

我加keyPressEventList類,並在這個事件中,我檢查,如果按下的鍵是Del鍵,如果是,稱爲_del_item方法。我還將addItems方法添加到mainWindow以將一些項目添加到listWidgets。這工作正常。我評論了你的連接del鍵。

只需複製並粘貼此代碼,運行它,並嘗試刪除的項目;)

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() 
相關問題