On Apr 2, 2010, at 5:24 PM, Phil Thompson wrote:

On Fri, 2 Apr 2010 17:05:15 -0700, Scott Frankel <[email protected] >
wrote:
In my attempts so far, my model instance
isn't calling its data() method.  (See attached.)

Because you've added a data() method to your QWidget subclass. You need a
sub-class of QSqlRelationalTableModel.


Thanks for the tip! There must still be another reason for the model's data() method not being called. Still doesn't work. I hope it's not as obvious as the first! (See revised model, attached.)

Thanks again!
Scott


#!/usr/bin/env python

from PyQt4 import QtCore, QtGui, QtSql

# column enumeration
ID, NAME, DESCRIPTION, COLOR_ID = range(4)



#-------------------------------------------------------------------------------
# class
#-------------------------------------------------------------------------------
class TestModel(QtSql.QSqlRelationalTableModel):
	def __init__(self, parent=None):
		QtSql.QSqlRelationalTableModel.__init__(self)



		# init
		# ------------------------------------------------
		self.displayList        = []



		# table model
		# ------------------------------------------------
		self.theModel = QtSql.QSqlRelationalTableModel(self)
		self.theModel.setTable("test")

		self.theModel.setRelation(COLOR_ID, QtSql.QSqlRelation("color", "color_id", "name"))
		self.theModel.setSort(ID, QtCore.Qt.AscendingOrder)
		self.theModel.setEditStrategy(QtSql.QSqlRelationalTableModel.OnManualSubmit)

		# column headers
		self.theModel.setHeaderData(ID, QtCore.Qt.Horizontal, QtCore.QVariant("ID"))
		self.theModel.setHeaderData(NAME, QtCore.Qt.Horizontal, QtCore.QVariant("Name"))
		self.theModel.setHeaderData(DESCRIPTION, QtCore.Qt.Horizontal, QtCore.QVariant("Description"))
		self.theModel.setHeaderData(COLOR_ID, QtCore.Qt.Horizontal, QtCore.QVariant("Color"))
		
		select = self.theModel.select()

		# TEST
		self.setDisplayList([1, 2])



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

	def setDisplayList(self, theList):
		self.displayList  = theList

		

	def data(self, index, role=QtCore.Qt.DisplayRole):
		print "testModel.data() ..."
		if not index.isValid() or \
		   not (0 <= index.row() < self.theModel.rowCount()) or \
		   not (index.row() in self.displayList):
			return QVariant()

        if role == Qt.DisplayRole:
            if column == NAME:
                return QVariant(self.theModel.record.value(NAME).toString())

			elif column == DESCRIPTION:
                return QVariant(self.theModel.record.value(DESCRIPTION).toString())

			elif column == COLOR_ID:
                return QVariant(self.theModel.record.value(COLOR_ID).toString())

		return QVariant





Phil










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

Reply via email to