1

Am subclassing QSqlRelationalTableModel.

class Titles(QSqlRelationalTableModel):
    def __init__(self):
        QSqlRelationalTableModel.__init__(self)

        self.setTable("titles")
        self.setRelation(self.fieldIndex("type"), QSqlRelation("title_type", "id", "type"))         
        self.select()

        print self.record(0).value("title").toString() # 1

    def data(self, i, role):
        if role == Qt.DisplayRole:
            print  self.record(0).value("title").toString() # 2 
            return self.record(0).value("title").toString() # 3

titles = Titles() 

print iswcTitlesModel.record(0).value("title").toString() # 4

All the above prints space (it returns the correct amount of rows, but only as blank space). If the data function is removed, #1 & #4 print. If doing return "string", all items are populated. The model is instantiated, and the intended return code works outside the model's definition. Tried multiple other SQL tables, all with a Relation, but even without a relation it doesn't work. Can anyone see what's going wrong?

user2422819
  • 177
  • 13

2 Answers2

0

Try using the domain within the relation

    self.setRelation(self.fieldIndex("type"), QSqlRelation("title_type", "title_type"."id", "title_type"."type"))         

Had similar problems, as "id" was in both tables

ngulam
  • 995
  • 1
  • 6
  • 11
  • Hi ngulam, thanks for the reply. The solution didn't make any difference, but am in Python 2.7. Even without a relation, the data still doesn't return, so it seems more why self.record(0).value("title").toString() isn't working (it does return, but only blank space), but return "string" does work. Regards – user2422819 Jun 06 '14 at 10:39
0

Found the answer:

def data(self, i, role):
        if role == Qt.DisplayRole:
            if i.column() == 1:
                return QVariant("col 1 custom data")
            else:
                return super(QSqlTableModel, self).data(i, role)

So if i.column() == the desired column, display whatever you want. Otherwise, return data that the super class (QSqlTableModel) had already mapped into the model.

user2422819
  • 177
  • 13