2014-03-27 3 views
3

ExtendedSelection이 활성화 된 단일 QListWidget이 있습니다 (여러 항목을 동시에 선택할 수 있음). 버튼을 클릭하면 listWidget에서 현재 항목을 가져 오기 위해 listWidget.currentItem()을 쿼리합니다. 질문 : listWidget에서 현재 선택된 모든 항목을 가져 오는 방법은 무엇입니까?Python : PyQt의 QListWidget에서 여러 선택된 항목을 쿼리하는 방법

from PyQt4 import QtGui, QtCore 
import sys, os 

class Dialog_01(QtGui.QMainWindow): 
    def __init__(self): 
     super(QtGui.QMainWindow,self).__init__() 

     myQWidget = QtGui.QWidget() 
     myBoxLayout = QtGui.QVBoxLayout() 
     myQWidget.setLayout(myBoxLayout) 
     self.setCentralWidget(myQWidget) 

     self.listWidget = QtGui.QListWidget() 
     self.listWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection) 

     for i in range(3): 
      item=QtGui.QListWidgetItem() 
      name='A'+'%04d'%i 
      item.setText(name)       
      self.listWidget.addItem(item) 

     myBoxLayout.addWidget(self.listWidget)  

     Button_01 = QtGui.QPushButton("Print Current Items") 
     Button_01.clicked.connect(self.printCurrentItems) 
     myBoxLayout.addWidget(Button_01) 


    def printCurrentItems(self): 
     print "Current Items are : ", self.listWidget.currentItem() 


if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    dialog_1 = Dialog_01() 
    dialog_1.show() 
    dialog_1.resize(720,480) 
    sys.exit(app.exec_()) 

답변

1

.selectedItems()

QTableWidget를위한 C++하지만 여기 QListWidget

QList<QTableWidgetItem*> selectedItems = matrixTable.selectedItems(); 
1

에 대해 동일한 작업 코드 (user3254944하기위한 감사합니다!) :

from PyQt4 import QtGui, QtCore 
import sys, os 

class Dialog_01(QtGui.QMainWindow): 
    def __init__(self): 
     super(QtGui.QMainWindow,self).__init__() 

     myQWidget = QtGui.QWidget() 
     myBoxLayout = QtGui.QVBoxLayout() 
     myQWidget.setLayout(myBoxLayout) 
     self.setCentralWidget(myQWidget) 

     self.listWidget = QtGui.QListWidget() 
     self.listWidget.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection) 

     for i in range(3): 
      item=QtGui.QListWidgetItem() 
      name='A'+'%04d'%i 
      item.setText(name)       
      self.listWidget.addItem(item) 

     myBoxLayout.addWidget(self.listWidget)  

     Button_01 = QtGui.QPushButton("Print Current Items") 
     Button_01.clicked.connect(self.printCurrentItems) 
     myBoxLayout.addWidget(Button_01) 


    def printCurrentItems(self): 
     print "Current Items are : ", self.listWidget.selectedItems() 


if __name__ == '__main__': 
    app = QtGui.QApplication(sys.argv) 
    dialog_1 = Dialog_01() 
    dialog_1.show() 
    dialog_1.resize(720,480) 
    sys.exit(app.exec_())