I have a strange problem. The following code works perfectly if I only map two widgets to the first two rows of a SQLITE table ("simpletable"). The widgets are filled at startup and the table is correctly updated when I edit the widgets and click the button and call mapper.submit().
However, as soon as I map a third widget, the widget values are filled properly at startup but I get an error when calling mapper.submit() and no mappings work anymore.
"simpletable" is just a simple set of rows and columns with text values:
"simpletable"
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
When the orientation is set to horizontal, the columns are properly mapped and everything works fine. Any suggestions for what the problem might be?
from PyQt4 import QtGui, QtCore, QtSql
import sys
def mapValues(mapper):
if not mapper.submit():
print "error!"
else:
print "ok!"
mapper.toFirst()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
mw = QtGui.QWidget()
db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("demo_mapper.db")
if not db.open():
print "did not open"
model = QtSql.QSqlTableModel()
model.setTable("simpletable")
model.select()
model.submitAll()
view = QtGui.QTableView()
view.setModel(model)
mapper = QtGui.QDataWidgetMapper()
mapper.setSubmitPolicy(QtGui.QDataWidgetMapper.ManualSubmit)
mapper.setModel(model)
mapper.setOrientation(QtCore.Qt.Vertical)
mapper.setCurrentIndex(0)
vbox = QtGui.QVBoxLayout()
vbox.addWidget(view)
qlinePropValue_0 = QtGui.QLineEdit()
qlinePropValue_1 = QtGui.QLineEdit()
qlinePropValue_2 = QtGui.QLineEdit()
mapper.addMapping(qlinePropValue_0, 0)
mapper.addMapping(qlinePropValue_1, 1)
### ERROR HAPPENS IF LINE BELOW IS INCLUDED ###
### NO ERROR IF LINE BELOW IS COMMENTED OUT
mapper.addMapping(qlinePropValue_2, 2)
mapper.toFirst()
view = QtGui.QTableView()
view.setModel(model)
vbox.addWidget(qlinePropValue_0)
vbox.addWidget(qlinePropValue_1)
vbox.addWidget(qlinePropValue_2)
mybutton = QtGui.QPushButton()
mybutton.setText("Press Me")
mybutton.clicked.connect(lambda: mapValues(mapper))
vbox.addWidget(mybutton)
mw.setLayout(vbox)
mw.show()
sys.exit(app.exec_())