I am trying to create an editable table in PyQt. Here is the code to display the table:
import sys from PyQt4 import QtGui, QtCore from PyQt4.QtCore import * from PyQt4.QtGui import * # données à représenter my_array = [['00','01','02'], ['10','11','12'], ['20','21','22']] def main(): app = QApplication(sys.argv) w = MyWindow() w.show() sys.exit(app.exec_()) # création de la vue et du conteneur class MyWindow(QWidget): def __init__(self, *args): QWidget.__init__(self, *args) tablemodel = MyTableModel(my_array, self) tableview = QTableView() tableview.setModel(tablemodel) layout = QVBoxLayout(self) layout.addWidget(tableview) self.setLayout(layout) # création du modèle class MyTableModel(QAbstractTableModel): def __init__(self, datain, parent = None, *args): QAbstractTableModel.__init__(self, parent, *args) self.arraydata = datain def rowCount(self, parent): return len(self.arraydata) def columnCount(self, parent): return len(self.arraydata[0]) def data(self, index, role): if not index.isValid(): return None elif role != Qt.DisplayRole: return None return (self.arraydata[index.row()][index.column()]) """ def setData(self, index, value): self.arraydata[index.row()][index.column()] = value return True def flags(self, index): return Qt.ItemIsEditable """ if __name__ == "__main__": main()
If I implement the setData and flags method, all the elements cannot even be selected ... What is the solution for editing the table? Thanks
setData
flags
I just found a solution, in the flag methods you need to return the value QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable
OR Override Qt::ItemFlags MyTableView::flags(const QModelIndex& index) const { Qt::ItemFlags flags = QAbstractTableModel::flags(index); flags |= Qt::ItemIsEditable; return flags; }
Source: https://habr.com/ru/post/921750/More articles:Jenkins / Hudson SVN issue - svnHow do you deal with data imbalance in SVM? - svmWhen a variable is passed to a function, why does the function only get a duplicate of the variable? - c ++Jenkins fails ALL TIME using hudson.util.IOException2 - svnXSL header and footer FO - headerShow calendar to select date in java - javaLibGDX - application crash when calling TiledMapRenderer.render () - javaLocation of a temporary Redis file for replication? - debianWhat is the maximum size of an image to be stored as a blob in sqlite database? - sqliteTwitter header battery - twitter-bootstrapAll Articles