Issue
I'm using PyQt5
and I would like to know how I can update the value of the selected items in a listview without having to delete them before, becauses it messes the ordering of the rows.
This is the code I'm using
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class App(QMainWindow):
def __init__(self):
super(App, self).__init__()
self.listView.setModel(QStandardItemModel())
self.update_btn.clicked.connect(self.update)
self.show()
def update(self):
if len(self.listView.selectedIndexes()) > 0:
items = self.listView.selectedIndexes()
for item in items:
row = item.row()
text, ok = QInputDialog.getText(self, 'Update Dialog', 'Enter new value')
if ok:
self.listView.model().takeRow(row)
self.listView.model().appendRow(QStandardItem(text))
app = QApplication(sys.argv)
window = App()
app.exec_()
Solution
You could try something like this:
def update(self):
if len(self.listView.selectedIndexes()) > 0:
indices = self.listView.selectedIndexes()
for index in indices:
item = self.listView.model().itemFromIndex(index)
text, ok = QInputDialog.getText(self, 'Update Dialog', 'Enter new value')
if ok:
item.setText(text)
Answered By - Heike
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.