Hi,

I have a form in which I use a QDataWidgetMapper object. When I try to submit values from the form back to the DB, no data gets written. However, mapping field values from my DB to the widgets on the form behaves flawlessly.

(See attached source code file. The Postgresql schema for the table in question is listed at the top of the file. It's only 3 fields long.)

Am I missing something obvious? I've tried to peek inside using queryModel.lastError(), but it's returning a memory address. Not sure how to get a string from this method.

I hope someone on the list can help shed light on this problem.

Thanks in advance!
Scott



#!/usr/bin/env python


#-------------------------------------------------------------------------------
# schema
#-------------------------------------------------------------------------------

#CREATE TABLE task_type (
#	task_type_id 		SERIAL		PRIMARY KEY,
#	name  				VARCHAR(30)	UNIQUE NOT NULL,
#	description			TEXT);




#-------------------------------------------------------------------------------
# imports
#-------------------------------------------------------------------------------
import sys
from PyQt4 import QtCore, QtGui, QtSql

MAC		   = "qt_mac_set_native_menubar" in dir()

# table field enumeration
ID, NAME, DESC = range(3)



#-------------------------------------------------------------------------------
# class
#-------------------------------------------------------------------------------
class TaskTypeForm(QtGui.QDialog):

	def __init__(self, parent=None):
		QtGui.QDialog.__init__(self)



		# init
		# ------------------------------------------------
		self.theFrame		    = QtGui.QFrame()
		self.theFrame.setFrameStyle(QtGui.QFrame.StyledPanel|QtGui.QFrame.Sunken)



		# type group
		# ------------------------------------------------
		self.typeGroup          = QtGui.QGroupBox()

		self.nameLabel  	    = QtGui.QLabel(self.tr("Name"))
		self.nameEdit           = QtGui.QLineEdit()

		self.descLabel  	    = QtGui.QLabel(self.tr("Description"))
		self.descEdit           = QtGui.QLineEdit()


		# layout
		self.typeLayout   = QtGui.QGridLayout()

		self.typeLayout.addWidget(self.nameLabel,            0, 0)
		self.typeLayout.addWidget(self.nameEdit,             0, 1)

		self.typeLayout.addWidget(self.descLabel,            1, 0)
		self.typeLayout.addWidget(self.descEdit,             1, 1)

		self.typeGroup.setLayout(self.typeLayout)

		

		# data model
		# ------------------------------------------------
		self.theModel           = QtSql.QSqlTableModel(self)
		self.theModel.setTable("task_type")
		self.theModel.setSort(NAME, QtCore.Qt.AscendingOrder)
		self.theModel.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
		self.theModel.setHeaderData(ID, QtCore.Qt.Horizontal, QtCore.QVariant("ID"))
		self.theModel.setHeaderData(NAME, QtCore.Qt.Horizontal, QtCore.QVariant("NAME"))
		self.theModel.setHeaderData(DESC, QtCore.Qt.Horizontal, QtCore.QVariant("DESC"))

		select = self.theModel.select()
		print "taskTypeTableModel select: ", select



		# data widget mapper
		# ------------------------------------------------
		self.mapper				= QtGui.QDataWidgetMapper(self)
		self.mapper.setSubmitPolicy(QtGui.QDataWidgetMapper.ManualSubmit)
		self.mapper.setModel(self.theModel)
		self.mapper.setItemDelegate(QtSql.QSqlRelationalDelegate(self))

		self.mapper.addMapping(self.nameEdit, NAME)
		self.mapper.addMapping(self.descEdit, DESC)

		self.mapper.toFirst()


		
		# buttons
		# ------------------------------------------------		
		self.newButton	        = QtGui.QPushButton("New")
		self.delButton	        = QtGui.QPushButton("Delete")
		self.commitButton		= QtGui.QPushButton("Commit")

		# disable delete button by default
		self.delButton.setEnabled(False)

		# buttons don't get tabbing focus on non-OSX platforms
		if not MAC:
			self.newButton.setFocusPolicy(QtCore.Qt.NoFocus)
			self.delButton.setFocusPolicy(QtCore.Qt.NoFocus)
			self.commitButton.setFocusPolicy(QtCore.Qt.NoFocus)


		# button group layout
		self.buttonGroup        = QtGui.QGroupBox()
		self.buttonLayout   	= QtGui.QHBoxLayout()
		self.buttonLayout.addStretch()		
		self.buttonLayout.addWidget(self.newButton)
		self.buttonLayout.addWidget(self.delButton)
		self.buttonLayout.addWidget(self.commitButton)
		self.buttonLayout.addStretch()
		self.buttonGroup.setLayout(self.buttonLayout)
		


		# main layout
		# ------------------------------------------------
		self.widgetLayout		= QtGui.QVBoxLayout()
		self.widgetLayout.addWidget(self.typeGroup)
		self.widgetLayout.addWidget(self.buttonGroup)
		self.widgetLayout.addStretch()

		self.formLayout		    = QtGui.QHBoxLayout()
		self.formLayout.addLayout(self.widgetLayout)

		self.theFrame.setLayout(self.formLayout)

		self.theLayout		    = QtGui.QVBoxLayout()
		self.theLayout.addWidget(self.theFrame)
		self.setLayout(self.theLayout)



		# signals/slots
		# ------------------------------------------------
		self.connect(self.newButton,	    QtCore.SIGNAL("clicked()"), self.newRecord)
		self.connect(self.commitButton,		QtCore.SIGNAL("clicked()"), self.commit)




	# methods
	# ------------------------------------------------

	def commit(self):
		row         = self.mapper.currentIndex()
		submit      = self.mapper.submit()
		self.mapper.setCurrentIndex(row)

		queryModel  = QtSql.QSqlQueryModel()
		error       = queryModel.lastError()
		print "error: ", error
		

		

	def newRecord(self):
		print "newRecord() ..."
		
		row         = self.theModel.rowCount()
		submit      = self.mapper.submit()
		self.theModel.insertRow(row)
		self.mapper.setCurrentIndex(row)

		self.nameEdit.setFocus()



	def deleteRecord(self):
		print "deleteRecord() ..."



		


#-------------------------------------------------------------------------------
# main
#-------------------------------------------------------------------------------
if __name__ == "__main__":

	app  = QtGui.QApplication(sys.argv)

	db   = QtSql.QSqlDatabase.addDatabase("QPSQL")
	db.setDatabaseName("fubarDB")
	if not db.open():
		QtGui.QMessageBox.warning(None, "Fubar",
			QtCore.QString("Database Error: %1").arg(db.lastError().text()))
		sys.exit(1)
	
	ok	 = db.open()
	print "db connection:	", ok

	form = TaskTypeForm()
	form.setWindowTitle("Data Widget Mapper Test")
	form.show()
	sys.exit(app.exec_())




_______________________________________________
PyQt mailing list    [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to