On Apr 3, 2010, at 5:16 PM, Scott Frankel wrote:


On Apr 2, 2010, at 5:05 PM, Scott Frankel wrote:

I'm hoping to be able to adapt a QSqlRelationalTableModel object to display only specified item rows directly. Using the view's setRowHidden() method is proving clunky.



Apologies in advance for taking up more bandwidth with this data model issue.

I'm having a chicken-egg problem with retrieving data from my subclassed QSqlRelationalTableModel object.

1)
If I try to retrieve a record using the model's record() method with a row argument, I get recursion errors: RuntimeError: maximum recursion depth exceeded

        item    = self.record(index.row())    # causes recursion errors

2)
Following example code for custom models [Summerfield chapt 14] and reviewing the select function in qsqlrelationaltablemodel.cpp, I've tried overriding the model's select() method and populating a list with item objects:

item = self.items[index.row()] # overriding select() masks data() method

For some reason, overriding select in my QSqlRelationalTableModel subclass prevents the data() method from being called.

Is there another way to both retrieve SQL data and manipulate which of it gets displayed in a view?

Thanks!
Scott



Example code attached:

#!/usr/bin/env python

from PyQt4 import QtCore, QtGui, QtSql

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



#-------------------------------------------------------------------------------
# class
#-------------------------------------------------------------------------------
class Test(object):

	def __init__(self, test_id, name, description, color_id):

		self.test_id			= test_id
		self.name		        = name
		self.description		= description
		self.color_id	        = color_id




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



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



		# table model
		# ------------------------------------------------
		self.setTable("test")

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

		# column headers
		self.setHeaderData(ID, QtCore.Qt.Horizontal, QtCore.QVariant("ID"))
		self.setHeaderData(NAME, QtCore.Qt.Horizontal, QtCore.QVariant("Name"))
		self.setHeaderData(DESCRIPTION, QtCore.Qt.Horizontal, QtCore.QVariant("Description"))
		self.setHeaderData(COLOR_ID, QtCore.Qt.Horizontal, QtCore.QVariant("Color"))
		
		select = self.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.rowCount()) or \
		   not (index.row() in self.displayList):
			return QVariant()

#		item    = self.record(index.row())    # causes recursion errors!!!
		item    = self.items[index.row()]     # overriding select() masks data() method!!!

        if role == Qt.DisplayRole:
            if column == NAME:
#                return QVariant(item.value(NAME).toString())
				return QVariant(item.name)

			elif column == DESCRIPTION:
#                return QVariant(item.value(DESCRIPTION).toString())
				return QVariant(item.description)
			
			elif column == COLOR_ID:
#                return QVariant(item.value(COLOR_ID).toString())
				return QVariant(item.color_id)
			
		return QVariant



	def select(self):
		print "testModel.select() ..."
		self.items	     = []
		query		     = QtSql.QSqlQuery()

		# notes
		queryStr	     = "SELECT item_id, name, description, color_id FROM test ORDER BY name"

		err              = query.exec_(queryStr)
		
		while query.next():
			item_id      = query.value(ID).toInt()[0]
			name         = query.value(NAME).toString()
			description  = query.value(DESCRIPTION).toString()			
			color_id     = query.value(COLOR_ID).toInt()[0]

			self.notes.append(Test(test_id, name, description, color_id))

		return err
	










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

Reply via email to