'problem inserting record in Sqlite table using QSQLTable model and QDataWidget mapper

I'm struggling with the code shown below that loads data from a SQLite table which has 3 columns ("codice" (primary key, autoincrement), "descrizione", "peso") using QSQLTable model with QDataWidget mapper.

My problem is I defined a QPushButton to insert new record in such table but I'm not able to get it work. I tried different ways using self.model.insertRows or self.model.insertRecord but I was not successfully. I'm not getting any error but the record is not inserted.

For sure there are conceptual errors I can't catch.

import sys

from PyQt5.QtCore import QSize, Qt
from PyQt5.QtSql import QSqlDatabase, QSqlTableModel

from PyQt5.QtWidgets import (
    QApplication,
    QComboBox,
    QDataWidgetMapper,
    QDoubleSpinBox,
    QFormLayout,
    QHBoxLayout,
    QLabel,
    QLineEdit,
    QMainWindow,
    QPushButton,
    QSpinBox,
    QTableView,
    QVBoxLayout,
    QWidget,
)
from connect_SQLITE import Database

db=Database.con
db.open()

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()

        form = QFormLayout()

        self.codice = QSpinBox()
        self.codice.setRange(0, 2147483647)
        self.codice.setDisabled(True)
        
        self.descrizione = QLineEdit()
        
        self.peso = QDoubleSpinBox()
        self.peso.setDecimals(5)
        self.peso.setRange(0, 9999.99999)
        self.peso.setSingleStep(0.01)

        form.addRow(QLabel("Codice Materiale"), self.codice)
        form.addRow(QLabel("Nome Materiale"), self.descrizione)
        form.addRow(QLabel("Peso specifico Materiale (Kg/dm3)"), self.peso)

        self.model = QSqlTableModel(db=db)
        self.mapper = QDataWidgetMapper()
        self.mapper.setModel(self.model)

        self.mapper.addMapping(self.codice, 0)
        self.mapper.addMapping(self.descrizione, 1)
        self.mapper.addMapping(self.peso, 2)

        self.model.setTable("Prova")
        self.model.select()

        self.mapper.toFirst()

        # tag::controls[]
        self.setMinimumSize(QSize(400, 400))

        controls = QHBoxLayout()

        prev_rec = QPushButton("Precedente")
        prev_rec.clicked.connect(self.mapper.toPrevious)

        next_rec = QPushButton("Successivo")
        next_rec.clicked.connect(self.mapper.toNext)

        ins_rec = QPushButton("Inserimento")
        ins_rec.clicked.connect(self.inserimento_materiale)
        
        save_rec = QPushButton("Salvataggio modifiche")
        save_rec.clicked.connect(self.mapper.submit)

        controls.addWidget(prev_rec)
        controls.addWidget(next_rec)
        controls.addWidget(ins_rec)
        controls.addWidget(save_rec)

        layout.addLayout(form)
        layout.addLayout(controls)

        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)
        # end::controls[]

    def inserimento_materiale(self):
        self.model.insertRows(self.model.rowCount(), 1)
        self.model.setData(self.model.index(0,1), self.descrizione.text())
        self.model.setData(self.model.index(0,2), self.peso)
        self.model.submit()
     

app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source