I'm trying to use QSqlRelationalTableModel to show records on a table that will grow at run-time.
What I get is that it works fine for records that are already present in sql tables but it doesn't for new lines that I add at run-time using .setData method. I'll attach to this message the original PyQt example about QSqlRelationalTableModel that I modified adding an "addRecord" function that shows the wrong behaviour I'm talking about. So I'd like to know what it the correct way to code that function. Thanks. Ciao. Licia
#!/bin/env python ############################################################################ ## ## Copyright (C) 2004-2005 Trolltech AS. All rights reserved. ## ## This file is part of the example classes of the Qt Toolkit. ## ## This file may be used under the terms of the GNU General Public ## License version 2.0 as published by the Free Software Foundation ## and appearing in the file LICENSE.GPL included in the packaging of ## this file. Please review the following information to ensure GNU ## General Public Licensing requirements will be met: ## http://www.trolltech.com/products/qt/opensource.html ## ## If you are unsure which license is appropriate for your use, please ## review the following information: ## http://www.trolltech.com/products/qt/licensing.html or contact the ## sales department at [EMAIL PROTECTED] ## ## This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ## WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ## ############################################################################ import sys from PyQt4 import QtCore, QtGui, QtSql def createConnection(): db = QtSql.QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName(":memory:") if not db.open(): QtGui.QMessageBox.critical(0, QtGui.qApp.tr("Cannot open database"), QtGui.qApp.tr("Unable to establish a database connection.\n" "This example needs SQLite support. Please read " "the Qt SQL driver documentation for information " "how to build it.\n\nClick Cancel to exit."), QtGui.QMessageBox.Cancle, QtGui.QMessageBox.NoButton) return False return True def initializeModel(model): model.setTable("employee") model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit) model.setRelation(2, QtSql.QSqlRelation("city", "id", "name")) model.setRelation(3, QtSql.QSqlRelation("country", "id", "name")) model.setHeaderData(0, QtCore.Qt.Horizontal, QtCore.QVariant(QtCore.QObject.tr(model, "ID"))) model.setHeaderData(1, QtCore.Qt.Horizontal, QtCore.QVariant(QtCore.QObject.tr(model, "Name"))) model.setHeaderData(2, QtCore.Qt.Horizontal, QtCore.QVariant(QtCore.QObject.tr(model, "City"))) model.setHeaderData(3, QtCore.Qt.Horizontal, QtCore.QVariant(QtCore.QObject.tr(model, "Country"))) model.select() def createView(title, model): view = QtGui.QTableView() view.setModel(model) view.setItemDelegate(QtSql.QSqlRelationalDelegate(view)) view.setWindowTitle(title) return view def createRelationalTables(): query = QtSql.QSqlQuery() query.exec_("create table employee(id int, name varchar(20), city int, country int)") query.exec_("insert into employee values(1, 'Espen', 5000, 47)") query.exec_("insert into employee values(2, 'Harald', 80000, 49)") query.exec_("insert into employee values(3, 'Sam', 100, 41)") query.exec_("create table city(id int, name varchar(20))") query.exec_("insert into city values(100, 'San Jose')") query.exec_("insert into city values(5000, 'Oslo')") query.exec_("insert into city values(80000, 'Munich')") query.exec_("create table country(id int, name varchar(20))") query.exec_("insert into country values(41, 'USA')") query.exec_("insert into country values(47, 'Norway')") query.exec_("insert into country values(49, 'Germany')") def addRecord(model ): row = model.rowCount() model.insertRow(row) val = (0, 'pippo', 1, 1) for column in range(1,4): model.setData(model.index(row,column), QtCore.QVariant(val[column])) if __name__ == "__main__": app = QtGui.QApplication(sys.argv) if not createConnection(): sys.exit(1) createRelationalTables() model = QtSql.QSqlRelationalTableModel() initializeModel(model) view = createView(model.tr("Relational Table Model"), model) addRecord(model ) view.show() sys.exit(app.exec_())
_______________________________________________ PyQt mailing list [email protected] http://www.riverbankcomputing.com/mailman/listinfo/pyqt
