'pyqt5 - QSQLTable model not committing new inserted row
I have the code shown below where in the first form there's a QTableView filled according to QSQLTable modelthe insertion. Upon the click of a QPushbutton widget, another form is displayed to insert new row in a db table (SQLite).
The problem I get is that the row is not inserted in the table as the submitAll() method does not work.
Where is my fault ? Thanks for any hints
class MainWindow(QWidget, Ui_f_tabella):
def __init__(self):
super().__init__()
self.setupUi(self)
self.w = None # No external window yet.
self.setWindowTitle("Gestione Risorse Interne")
self.pb_Ins.setText("Inserimento Nuova Risorsa Interna")
self.model = QSqlTableModel(self)
self.model.setTable("tb_RisorseInterne")
self.model.setEditStrategy(QSqlTableModel.OnFieldChange)
self.model.setHeaderData(0, Qt.Horizontal, "CDL")
self.model.setHeaderData(1, Qt.Horizontal, "Desc_CDL")
self.model.setHeaderData(2, Qt.Horizontal, "Tipo")
self.model.setHeaderData(3, Qt.Horizontal, "Desc_2")
self.model.setHeaderData(4, Qt.Horizontal, "Costi")
self.model.setHeaderData(5, Qt.Horizontal, "Troncone")
self.model.setHeaderData(6, Qt.Horizontal, "BarraUtile")
self.model.setHeaderData(7, Qt.Horizontal, "SovrametalloPerTaglio")
self.model.setHeaderData(8, Qt.Horizontal, "CambioUtensile")
self.model.setHeaderData(9, Qt.Horizontal, "TempoCSAutomatico")
self.model.setHeaderData(10, Qt.Horizontal, "TempoRiposizCreSecPass")
self.model.setHeaderData(11, Qt.Horizontal, "TempoRiposizCreCaricMANUALE")
self.model.setHeaderData(12, Qt.Horizontal, "NrGiriMax-RPM")
self.model.select()
self.tView.setModel(self.model)
self.tView.setSelectionMode(QTableView.SingleSelection)
self.tView.setSelectionBehavior(QTableView.SelectRows)
self.pb_Ins.clicked.connect(self.inserimento_ri)
def inserimento_ri(self):
if self.w is None:
self.w = InserimentoRisorsaInterna()
self.w.show()
self.w.pb_conf.clicked.connect(self.conferma_inserimento_risorsa_interna)
else:
self.w.close()
self.w = None # Discard reference, close window.
def conferma_inserimento_risorsa_interna(self):
"""Aggiunta nuova risorsa"""
self.data = []
for field in (self.w.lE_cdl, self.w.lE_descCDL, self.w.lE_tipo, self.w.lE_desc2,
self.w.dSB_costi, \
self.w.sB_troncone, self.w.sB_barra_utile, self.w.sB_sovrametallo, \
self.w.dSB_cambio_utensile, self.w.sB_tempo_carico_auto, \
self.w.dSB_tempo_rip_cre_2pass, self.w.dSB_tempo_rip_cre_car_man, \
self.w.sB_nr_giri_max):
if not field.text():
QMessageBox.critical(
self,
"Errore!",
f"Inserimento non consentito per mancanza di informazioni"
# f"Inserire il valore {field.objectName()}",
)
self.data = None # Reset .data
return
self.data.append(field.text())
if not self.data:
return
rows = self.model.rowCount()
self.model.insertRows(rows, 1)
for column_index, field in enumerate(self.data):
self.model.setData(self.model.index(rows, column_index + 1), field)
self.model.submitAll()
self.model.select()
class InserimentoRisorsaInterna(QDialog, Ui_InsRisorseInterne):
def __init__(self):
super().__init__()
self.setupUi(self)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(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 |
|---|
