class Shell(QMainWindow): def __init__(self): # constructor super().__init__() # call the parent's constructor # Test data in a model self.users = ['User 1', 'User 2', 'User 3'] self.lv_model = QStringListModel() self.lv_model.setStringList(self.users) # Create the main window content widget w = QWidget() # Setup the rest of the main window appearance self.setGeometry(300, 300, 640, 480) self.setWindowTitle('PySide2 Listview Experiments') self.setWindowIcon(QIcon('assets/icons/moon_64x64.png')) # Create and set the main layout layout = QVBoxLayout() w.setLayout(layout) # Set the layout of the main window content widget self.setCentralWidget(w) # Create and add components to the layout self.lv_label = QLabel('QListView with QStringListModel') layout.addWidget(self.lv_label) self.lv = QListView() self.lv.setSelectionMode( QListView.MultiSelection) # single selection is the default self.lv.setModel(self.lv_model) self.lv.selectionModel().selectionChanged.connect(self.item_selected) layout.addWidget(self.lv) self.button = QPushButton('Update model') self.button.clicked.connect(self.change_model) layout.addWidget(self.button) self.selected_label = QLabel('Selected item: none') layout.addWidget(self.selected_label) layout.addStretch(1) self.show() # display the UI def change_model(self): the_list = self.lv_model.stringList() the_list.append('Another User') self.lv_model.setStringList(the_list) def item_selected(self): # Get the current index (a QModelIndex object) from the QListView # From the index, get the data (a QVariant) that you can convert to a QString index = self.lv.currentIndex() # returns the primary QModelIndex indices = self.lv.selectedIndexes() # returns a list of QModelIndex selected_text = '' for i in indices: selected_text += i.data( ) + '\n' # use .data() to get the value from QModelIndex self.selected_label.setText(selected_text)
class IconColorEditor(QDialog): """An editor to let the user select an icon and a color for an object_class. """ def __init__(self, parent): """Init class.""" super().__init__(parent) # , Qt.Popup) icon_size = QSize(32, 32) self.icon_mngr = IconListManager(icon_size) self.setWindowTitle("Select icon and color") self.icon_widget = QWidget(self) self.icon_list = QListView(self.icon_widget) self.icon_list.setViewMode(QListView.IconMode) self.icon_list.setIconSize(icon_size) self.icon_list.setResizeMode(QListView.Adjust) self.icon_list.setItemDelegate(_IconPainterDelegate(self)) self.icon_list.setMovement(QListView.Static) self.icon_list.setMinimumHeight(400) icon_widget_layout = QVBoxLayout(self.icon_widget) icon_widget_layout.addWidget(QLabel("Font Awesome icons")) self.line_edit = QLineEdit() self.line_edit.setPlaceholderText("Search icons for...") icon_widget_layout.addWidget(self.line_edit) icon_widget_layout.addWidget(self.icon_list) self.color_dialog = QColorDialog(self) self.color_dialog.setWindowFlags(Qt.Widget) self.color_dialog.setOption(QColorDialog.NoButtons, True) self.color_dialog.setOption(QColorDialog.DontUseNativeDialog, True) self.button_box = QDialogButtonBox(self) self.button_box.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok) top_widget = QWidget(self) top_layout = QHBoxLayout(top_widget) top_layout.addWidget(self.icon_widget) top_layout.addWidget(self.color_dialog) layout = QVBoxLayout(self) layout.addWidget(top_widget) layout.addWidget(self.button_box) self.proxy_model = QSortFilterProxyModel(self) self.proxy_model.setSourceModel(self.icon_mngr.model) self.proxy_model.filterAcceptsRow = self._proxy_model_filter_accepts_row self.icon_list.setModel(self.proxy_model) self.setAttribute(Qt.WA_DeleteOnClose) self.connect_signals() def _proxy_model_filter_accepts_row(self, source_row, source_parent): """Overridden method to filter icons according to search terms. """ text = self.line_edit.text() if not text: return QSortFilterProxyModel.filterAcceptsRow( self.proxy_model, source_row, source_parent) searchterms = self.icon_mngr.model.index( source_row, 0, source_parent).data(Qt.UserRole + 1) return any([text in term for term in searchterms]) def connect_signals(self): """Connect signals to slots.""" self.line_edit.textEdited.connect(self.proxy_model.invalidateFilter) self.button_box.accepted.connect(self.accept) self.button_box.rejected.connect(self.reject) def set_data(self, data): icon_code, color_code = interpret_icon_id(data) self.icon_mngr.init_model() for i in range(self.proxy_model.rowCount()): index = self.proxy_model.index(i, 0) if index.data(Qt.UserRole) == icon_code: self.icon_list.setCurrentIndex(index) break self.color_dialog.setCurrentColor(QColor(color_code)) def data(self): icon_code = self.icon_list.currentIndex().data(Qt.UserRole) color_code = self.color_dialog.currentColor().rgb() return make_icon_id(icon_code, color_code)
class InstallPluginDialog(QDialog): item_selected = Signal(str) def __init__(self, parent): """Initialize class""" super().__init__(parent) self.setWindowTitle('Install plugin') QVBoxLayout(self) self._line_edit = QLineEdit(self) self._line_edit.setPlaceholderText("Search registry...") self._list_view = QListView(self) self._model = QSortFilterProxyModel(self) self._source_model = _InstallPluginModel(self) self._model.setSourceModel(self._source_model) self._model.setFilterCaseSensitivity(Qt.CaseInsensitive) self._list_view.setModel(self._model) self._timer = QTimer(self) self._timer.setInterval(200) self._button_box = QDialogButtonBox(self) self._button_box.setStandardButtons(QDialogButtonBox.Cancel | QDialogButtonBox.Ok) self._button_box.button(QDialogButtonBox.Ok).setEnabled(False) self.layout().addWidget(self._line_edit) self.layout().addWidget(self._list_view) self.layout().addWidget(self._button_box) self.setAttribute(Qt.WA_DeleteOnClose) self.setMinimumWidth(400) self._button_box.button(QDialogButtonBox.Cancel).clicked.connect( self.close) self._button_box.button(QDialogButtonBox.Ok).clicked.connect( self._handle_ok_clicked) self._list_view.doubleClicked.connect(self._emit_item_selected) self._list_view.selectionModel().selectionChanged.connect( self._update_ok_button_enabled) self._line_edit.textEdited.connect(self._handle_search_text_changed) self._timer.timeout.connect(self._filter_model) def populate_list(self, names): for name in names: self._source_model.appendRow(QStandardItem(name)) @Slot(str) def _handle_search_text_changed(self, _text): self._timer.start() def _filter_model(self): self._model.setFilterRegExp(self._line_edit.text()) @Slot(bool) def _handle_ok_clicked(self, _=False): index = self._list_view.currentIndex() self._emit_item_selected(index) @Slot("QModelIndex") def _emit_item_selected(self, index): if not index.isValid(): return self.item_selected.emit(index.data(Qt.DisplayRole)) self.close() @Slot("QItemSelection", "QItemSelection") def _update_ok_button_enabled(self, _selected, _deselected): on = self._list_view.selectionModel().hasSelection() self._button_box.button(QDialogButtonBox.Ok).setEnabled(on)