PDA

View Full Version : primeInsert() and setValue not working QSqlRelationalTableModel



lukeQt
4th May 2015, 22:29
Hi Everyone,

I am using QSqlRelationalTableModel. I have subclass the model. I am using editStrategy onManualSubmit.
This is the code I have so far. What is not working is the default values. I am using primeInsert so that dataChanged is not emitted each time insertRows is called. Why is setValue not working? Is this almost right?


class AppField(QtGui.QDialog, ui_app_field.Ui_AppFieldDialog):
def __init__(self, database = None, table = None, parent = None):
super(AppField, self).__init__(parent)
self._table = table
self._dbfname = database
self.setupUi(self)
# subclassed sqlrelationaltablemodel.
self.model = AppModel(table = self._table, parent = self)
self.app_field_tableView.setModel(self.model)

self.add_row_pushButton.clicked.connect(self.add_r ow)
self.model.primeInsert.connect(self.default_val)
self.model.dataChanged.connect(self.submit)

def submit(self, index):
print "help"
id = self.model.data(self.model.index(index.row(), ID))
app_alias = self.model.data(self.model.index(index.row(), ALIAS))
app_field = self.model.data(self.model.index(index.row(), APP_FIELD))
app_def = self.model.data(self.model.index(index.row(), APP_DEF))
if self.model.isDirty(self.model.index(index.row(), ALIAS)) or app_alias != "Event Alias Name":
if self.model.isDirty(self.model.index(index.row(), APP_FIELD)) or app_field != "Event name":
if self.model.isDirty(self.model.index(index.row(), APP_DEF)) or app_def != "Event Definition":
self.model.submitAll()
if self.model.lastError().isValid():
if self.model.lastError().text() == "column app_field_alias is not unique Unable to fetch row":
self.model.revertAll()
ms = self.model.lastError().text()


def default_val(self, rec):
"""
add default values to the table. I can not use setData because that
will emit dataChanged.
This is not working.
"""
query = QtSql.QSqlQuery()
query.exec_("SELECT id from in_gtlf")
if query.next():
gtlf_id = query.value(0).toInt()[0]
record = self.model.record(rec)
record.setValue(1, "Event Alias Name")
record.setValue(2, "Event name")
record.setValue(3, "Event Definition")
record.setValue(4, gtlf_id)



def add_row(self):
"""
This method adds rows to the model.
"""
row = self.model.rowCount()
if self.model.lastError().isValid():
ms = self.model.lastError().text()
QtGui.QMessageBox.warning(self,"warning", ms, QtGui.QMessageBox.Ok)
else:
self.model.insertRows(row, 1)
self.model.submitAll()
index = self.model.index(row, 1)
if index.isValid():
self.app_field_tableView.edit(index)
self.resizeRow()