2016-12-24 1 views
0

저는 PySide를 사용하여 GUI를 프로그래밍하고 있습니다. 현재 사용자가 많은 데이터 파일이있는 디렉토리를 선택하게했습니다. 이 파일 이름을 목록에로드합니다. GUI에서 파일 이름 목록을 보여주는 팝업 메뉴를 표시하여 사용자가 계속 진행할 파일 하나 또는 여러 개를 선택할 수있게하고 싶습니다. 지금 사용 중입니다.PySide 팝업 목록 및 다중 선택

items, ok = QInputDialog.getItem(self, "Select files", "List of files", datafiles, 0, False) 

이 옵션을 선택하면 사용자가 하나의 파일 만 선택할 수 있습니다. 어떻게 사용자에게 항목 목록을 표시하고 원하는만큼 강조 표시 한 다음 목록을 반환 할 수 있습니까?

감사합니다!

+0

QInputDialog 클래스는 ** 한 ** 가치를 얻을 수있는 간단한 편의 대화 상자를 제공합니다 사용자로부터 – eyllanesc

답변

0

QInputDialog 클래스는 단일 값을 얻기위한 간단한 대화 상자를 제공하지만 사용자 지정 대화 상자를 만들 수 있습니다. 클릭 버튼 후

import sys 

from PySide.QtCore import Qt 
from PySide.QtGui import QApplication, QDialog, QDialogButtonBox, QFormLayout, \ 
    QLabel, QListView, QPushButton, QStandardItem, QStandardItemModel, QWidget 


class MyDialog(QDialog): 
    def __init__(self, title, message, items, parent=None): 
     super(MyDialog, self).__init__(parent=parent) 
     form = QFormLayout(self) 
     form.addRow(QLabel(message)) 
     self.listView = QListView(self) 
     form.addRow(self.listView) 
     model = QStandardItemModel(self.listView) 
     self.setWindowTitle(title) 
     for item in items: 
      # create an item with a caption 
      standardItem = QStandardItem(item) 
      standardItem.setCheckable(True) 
      model.appendRow(standardItem) 
     self.listView.setModel(model) 

     buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel, Qt.Horizontal, self) 
     form.addRow(buttonBox) 
     buttonBox.accepted.connect(self.accept) 
     buttonBox.rejected.connect(self.reject) 

    def itemsSelected(self): 
     selected = [] 
     model = self.listView.model() 
     i = 0 
     while model.item(i): 
      if model.item(i).checkState(): 
       selected.append(model.item(i).text()) 
      i += 1 
     return selected 


class Widget(QWidget): 
    def __init__(self, parent=None): 
     super(Widget, self).__init__(parent=parent) 
     self.btn = QPushButton('Select', self) 
     self.btn.move(20, 20) 
     self.btn.clicked.connect(self.showDialog) 
     self.setGeometry(300, 300, 290, 150) 
     self.setWindowTitle('Input dialog') 

    def showDialog(self): 
     items = [str(x) for x in range(10)] 
     dial = MyDialog("Select files", "List of files", items, self) 
     if dial.exec_() == QDialog.Accepted: 
      print(dial.itemsSelected()) 

if __name__ == '__main__': 
    app = QApplication(sys.argv) 
    ex = Widget() 
    ex.show() 
    sys.exit(app.exec_()) 

enter image description here

:

enter image description here

출력 :

['1', '2', '4', '5'] 
+0

좋아요! 이것은 내가 뭘 찾고 있었는지, 정말 고마워 :) – user1408329