I am trying to run some PyQt6 Model/View Tutorial.
I can't get the dataChange function to work. I tried everything from the web.
model.py
COLS = 3
ROWS = 2
class MyModel(QAbstractTableModel):
editCompleted = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self._grid_data = [["" for y in range(COLS)] for x in range(ROWS)]
print(self._grid_data)
def rowCount(self, parent=None):
return ROWS
def columnCount(self, parent=None):
return COLS
def data(self, index, role=Qt.ItemDataRole.DisplayRole):
row = index.row()
col = index.column()
print(f"row {row}, col{col}, role {role}")
if role == Qt.ItemDataRole.DisplayRole:
if row == 0 and col == 1:
return "<--left"
if row == 1 and col == 1:
return "right-->"
return f"Row{row}, Column{col+1}"
return None
def headerData(self, section, orientation, role=Qt.ItemDataRole.DisplayRole):
if role != Qt.ItemDataRole.DisplayRole:
return None
if orientation == Qt.Orientation.Horizontal:
return f"Column {section}"
return f"Row {section}"
def setData(self, index, value, role):
if role != Qt.ItemDataRole.EditRole or not self.checkIndex(index):
return False
self._grid_data[index.row()][index.column()] = value
result = " ".join(chain(*self._grid_data))
self.editCompleted.emit(result)
self.dataChanged.emit(index, index, [Qt.ItemDataRole.EditRole])
return True
def flags(self, index):
return Qt.ItemFlag.ItemIsEditable | super().flags(index)
mainWindow.py
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self._table_view = QTableView(self)
self.setCentralWidget(self._table_view)
myModel = mymodel.MyModel(self)
self._table_view.setModel(myModel)
# transfer changes to the model to the window title
myModel.editCompleted.connect(self.show_window_title)
@pyqtSlot(str)
def show_window_title(self, title):
self.setWindowTitle(title)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec())
The title is changing but data inside cell not changing in the end I want to work with Db and it needed to be dynamic.
Show list of files from the database