2016-12-24 30 views
0

私はPySideを使用してGUIをプログラミングしています。私は現在、ユーザーが多数のデータファイルを含むディレクトリを選択しています。これらのファイル名をリストにロードします。 GUIでファイル名のリストを示すポップアップメニューを表示して、ユーザが1つ、多くの、またはすべてのファイルを選択して進むことを許可します。今すぐ使用しています。リストと複数の選択を表示するPySideポップアップ

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

これは、ユーザーが複数のファイルではなく1つのファイルを選択できるようにします。どのようにしてユーザーにアイテムのリストを表示し、それらのアイテムを必要な数だけ強調表示させてからリストを返すことができますか?

ありがとうございます!

+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

関連する問題