在 QListView 中保存和恢复选择
Posted
技术标签:
【中文标题】在 QListView 中保存和恢复选择【英文标题】:Saving and restoring selection in QListView 【发布时间】:2019-11-28 21:04:02 【问题描述】:拥有QListView
想要保存并恢复QListView
中的选择。单击“确定”按钮时,保存所选项目,再次运行时,恢复所选项目并显示?如何实现?
显示
代码
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setFont(QtGui.QFont("Helvetica", 8, QtGui.QFont.Normal, italic=False))
self.setWindowTitle("Getting selected item")
lay = QtWidgets.QVBoxLayout(self)
self.button = QtWidgets.QPushButton("Okay")
self.button.clicked.connect(self.on_click_ok)
self.listView = QtWidgets.QListView(editTriggers=QtWidgets.QAbstractItemView.NoEditTriggers,
selectionMode=QtWidgets.QAbstractItemView.SingleSelection,
selectionBehavior=QtWidgets.QAbstractItemView.SelectRows,)
lay.setContentsMargins(10,10,10,10)
lay.addWidget(self.listView)
self.entry = QtGui.QStandardItemModel()
self.listView.setModel(self.entry)
self.listView.setSpacing(4)
self.listView.setFont(QtGui.QFont("Helvetica",9, QtGui.QFont.Normal, italic=False))
self.listView.setUniformItemSizes(True)
self.listView.setFixedSize(300, 150)
for i, text in enumerate(["Itemname1", "Itemname2", "Itemname3", "Itemname4", "Itemname5"]):
it = QtGui.QStandardItem(text)
self.entry.appendRow(it)
self.listView.selectionModel().currentChanged.connect(self.on_row_changed)
selection = self.listView.selectionModel().selectedRows()
ix = self.entry.index(0, 0)
sm = self.listView.selectionModel()
sm.select(ix, QtCore.QItemSelectionModel.Select)
Code_Group = QtWidgets.QGroupBox(self)
Code_Group.setTitle("&Selecting and deselecting")
Code_Group.setLayout(lay)
Vlay = QtWidgets.QVBoxLayout(self)
Vlay.addWidget(Code_Group)
Vlay.addWidget(self.button, alignment=QtCore.Qt.AlignCenter)
Vlay.setSizeConstraint(Vlay.SetFixedSize)
self.setWindowFlag(QtCore.Qt.WindowMaximizeButtonHint, True)
self._INDEX = 0
self.SELECTED = self.listView.selectedIndexes()
print(self.SELECTED)
def on_row_changed(self, current, previous):
self._INDEX = current.row()
print('Row %d selected' % current.row())
def on_click_ok(self):
print('Row index:', self._INDEX, 'After:')
self.accept()
def closeEvent(self,event):
super(Widget, self).closeEvent(event)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
【问题讨论】:
【参考方案1】:考虑到my previous answer,我已经实现了类似的逻辑,其中项目使用 QDataStream 保存,并保存选定的行然后恢复它。
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Widget(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Widget, self).__init__(parent)
self.setFont(QtGui.QFont("Helvetica", 8, QtGui.QFont.Normal, italic=False))
self.setWindowTitle("Getting selected item")
lay = QtWidgets.QVBoxLayout(self)
self.button = QtWidgets.QPushButton("Okay")
self.button.clicked.connect(self.accept)
self.listView = QtWidgets.QListView()
lay.setContentsMargins(10, 10, 10, 10)
lay.addWidget(self.listView)
self.entry = QtGui.QStandardItemModel()
self.listView.setModel(self.entry)
self.listView.setSpacing(4)
self.listView.setFont(
QtGui.QFont("Helvetica", 9, QtGui.QFont.Normal, italic=False)
)
self.listView.setUniformItemSizes(True)
self.listView.setFixedSize(300, 150)
Code_Group = QtWidgets.QGroupBox()
Code_Group.setTitle("&Selecting and deselecting")
Code_Group.setLayout(lay)
Vlay = QtWidgets.QVBoxLayout(self)
Vlay.addWidget(Code_Group)
Vlay.addWidget(self.button, alignment=QtCore.Qt.AlignCenter)
Vlay.setSizeConstraint(Vlay.SetFixedSize)
self.setWindowFlag(QtCore.Qt.WindowMaximizeButtonHint, True)
self.read_settings()
self.accepted.connect(self.write_settings)
def closeEvent(self, event):
print("closeEvent")
self.write_settings()
super(Widget, self).closeEvent(event)
def read_settings(self):
settings = QtCore.QSettings("file.ini", QtCore.QSettings.IniFormat)
settings.beginGroup("listView")
model = self.listView.model()
if settings.contains("items"):
editTriggers = settings.value(
"editTriggers", type=QtWidgets.QAbstractItemView.EditTriggers
)
selectionMode = settings.value(
"selectionMode", type=QtWidgets.QAbstractItemView.SelectionMode
)
selectionBehavior = settings.value(
"selectionBehavior", type=QtWidgets.QAbstractItemView.SelectionBehavior
)
self.listView.setEditTriggers(editTriggers)
self.listView.setSelectionMode(selectionMode)
self.listView.setSelectionBehavior(selectionBehavior)
items = settings.value("items", type=QtCore.QByteArray)
stream = QtCore.QDataStream(items, QtCore.QIODevice.ReadOnly)
while not stream.atEnd():
it = QtGui.QStandardItem()
stream >> it
model.appendRow(it)
selecteditems = settings.value("selecteditems", type=list)
for row in selecteditems:
index = model.index(row, 0)
self.listView.selectionModel().select(
index, QtCore.QItemSelectionModel.Select
)
else:
for text in [
"Itemname1",
"Itemname2",
"Itemname3",
"Itemname4",
"Itemname5",
]:
it = QtGui.QStandardItem(text)
model.appendRow(it)
self.listView.setEditTriggers(
QtWidgets.QAbstractItemView.NoEditTriggers
)
self.listView.setSelectionMode(
QtWidgets.QAbstractItemView.SingleSelection
)
self.listView.setSelectionBehavior(
QtWidgets.QAbstractItemView.SelectRows
)
settings.endGroup()
def write_settings(self):
settings = QtCore.QSettings("file.ini", QtCore.QSettings.IniFormat)
settings.beginGroup("listView")
model = self.listView.model()
settings.setValue("editTriggers", self.listView.editTriggers())
settings.setValue("selectionMode", self.listView.selectionMode())
settings.setValue("selectionBehavior", self.listView.selectionBehavior())
items = QtCore.QByteArray()
stream = QtCore.QDataStream(items, QtCore.QIODevice.WriteOnly)
for i in range(model.rowCount()):
it = model.item(i)
stream << it
settings.setValue("items", items)
selecteditems = []
for index in self.listView.selectionModel().selectedRows():
selecteditems.append(index.row())
settings.setValue("selecteditems", selecteditems)
settings.endGroup()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
【讨论】:
非常感谢您的努力和回复。感觉编写长代码来做到这一点。以上是关于在 QListView 中保存和恢复选择的主要内容,如果未能解决你的问题,请参考以下文章