[PyQt] Repositioning columns in table view
Andrew Douglas
andy.douglas at gmail.com
Fri Jan 4 12:56:03 GMT 2008
Hi
I'm trying to adjust the positions of columns in a table view linked
to a QSqlRelationalTableModel. I want to be able to add an additional
column with static data in and also reorder the columns in the view
differently to the sequence the fields are returned from the select
query.
Here is what I'm doing so far:
class browser(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Browser()
self.ui.setupUi(self)
db = self.setupDB()
if db.open():
self.initDBModel(db)
else:
print 'Failed to open database'
def setupDB(self):
db = QtSql.QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("127.0.0.1")
db.setDatabaseName("blah")
db.setUserName("foo")
db.setPassword("bar")
return db
def initDBModel(self, db):
relModel = QtSql.QSqlRelationalTableModel(self, db)
relModel.setTable("mumble")
relModel.setRelation(3, QtSql.QSqlRelation("person", "id", "name"))
relModel.setRelation(4, QtSql.QSqlRelation("person", "id", "name"))
relModel.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
relModel.select()
relModel.removeColumn(0)
#relModel.insertColumn(3)
relModel.setHeaderData(2, QtCore.Qt.Horizontal,
QtCore.QVariant("Attendee1"))
relModel.setHeaderData(4, QtCore.Qt.Horizontal,
QtCore.QVariant("Attendee2"))
self.ui.resultsTable.setModel(relModel)
self.ui.resultsTable.setItemDelegate(QtSql.QSqlRelationalDelegate(self.ui.resultsTable))
I can successfully remove the primary key field with the
relModel.removeColumn(0) statement, but when I try and insert an extra
column using the relModel.insertColumn(3), I correctly get the extra
column but the data after the insert is shifted 2 columns to the right
(and I therefore also lose the data from the final field).
It seems that I'm missing something. Can someone point me in the right
direction, pls.
Andy
More information about the PyQt
mailing list