Reputation: 105
There are two QListView widgets and two QPushButtons (disable and enable). "list1" on the left hand side contains list of items. "list2" initially is empty. Disable button should copy selected items of "list1" to "list2" and should disable the selected items in "list1". Enable button should remove selected items in "list2" and should enable items in "list1" that are equal to the selected items in "list2". My question is how can I disable/enable items in "list1"?
Below are the scrrenshots and the code.
"list1" on the left hand side
"list2" on the right hand side
Copy selected items in "list1" to "list2" and disable selected items in "list1" (in this case items "Apple" and "Orange" were selected in "list1" and were copied to "list2" and should be disabled in "list1")
Remove selected items in "list2" and enable items in "list1" that are equal to items selected in "list2" (in this case "Orange" was selected in "list2" and was removed from "list2", and was enabled in "list1")
Here is the code:
import sys
from PySide6.QtWidgets import (QApplication, QMainWindow, QListView, QHBoxLayout, QWidget, QVBoxLayout, QPushButton,
QAbstractItemView)
from PySide6.QtCore import Qt, QAbstractListModel
class List1Model (QAbstractListModel):
"""
Updates view in QListView.
"""
def __init__(self, list_items):
""" Model initializer."""
super().__init__()
self._list_items = list_items
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable
def rowCount(self, parent=None):
return len(self._list_items)
#
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return self._list_items[index.row()]
class List2Model (QAbstractListModel):
"""
Updates view in QListView.
"""
def __init__(self, list_items=None):
""" Model initializer."""
super().__init__()
self._list_items = list_items or []
def flags(self, index):
return Qt.ItemIsEnabled | Qt.ItemIsSelectable
def rowCount(self, parent=None):
return len(self._list_items)
#
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return self._list_items[index.row()]
def disable():
indexes = list1.selectedIndexes()
if indexes:
rows = [item.row() for item in indexes]
selected_items = [list1.model()._list_items[row] for row in rows]
list2.model()._list_items.extend(selected_items)
list2.model().layoutChanged.emit()
list1.clearSelection()
def enable():
indexes = list2.selectedIndexes()
if indexes:
rows = [item.row() for item in indexes]
selected_items = [list2.model()._list_items[row] for row in rows]
list2.model()._list_items = [i for i in list2.model()._list_items if i not in selected_items]
list2.model().layoutChanged.emit()
list2.clearSelection()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QMainWindow()
window.setGeometry(100, 100, 200, 100)
list1 = QListView()
list2 = QListView()
list1.setSelectionMode(QAbstractItemView.ExtendedSelection)
list2.setSelectionMode(QAbstractItemView.ExtendedSelection)
central_widget = QWidget()
disable_btn = QPushButton("Disable >")
enable_btn = QPushButton("< Enable")
layout = QVBoxLayout(central_widget)
list_sublayout = QHBoxLayout()
list_sublayout.addWidget(list1)
list_sublayout.addWidget(list2)
layout.addLayout(list_sublayout)
layout.addWidget(disable_btn)
layout.addWidget(enable_btn)
window.setCentralWidget(central_widget)
list_items = ["Apple", "Orange", "Grape"]
list1_model = List1Model(list_items)
list2_model = List2Model()
list1.setModel(list1_model)
list2.setModel(list2_model)
disable_btn.pressed.connect(disable)
enable_btn.pressed.connect(enable)
window.show()
sys.exit(app.exec())
Upvotes: 1
Views: 64