I started to learn Qt4 Model / View Programming and I have a beginner question.
I have a simple application that shows a sqlite table in a QTableView :
class Model(QtSql.QSqlTableModel): def __init__(self, parent=None): super(Model, self).__init__(parent) self.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange) self.setTable("test") self.select() class App(QtGui.QMainWindow): def __init__(self, model): QtGui.QMainWindow.__init__(self) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.tableView.setModel(model) if __name__ == "__main__": myDb = QtSql.QSqlDatabase.addDatabase("QSQLITE") myDb.setDatabaseName("test.db") if not myDb.open(): print 'FIXME' model = Model() app = QtGui.QApplication(sys.argv) window = App(model) window.show() sys.exit(app.exec_())
Here's what the database looks like:
sqlite> create table test (a INTEGER, b INTEGER, c STRING); sqlite> insert into test VALUES(1, 2, "xxx"); sqlite> insert into test VALUES(6, 7, "yyy");
So, I get something like:
+---+---+-----+ | a | b | c | +---+---+-----+ | 1 | 2 | xxx | +---+---+-----+ | 6 | 7 | yyy | +---+---+-----+
Is it possible to modify Model to have something like a virtual column in QTableView ? For example, something like:
+---+---+-----+-----+ | a | b | sum | c | +---+---+-----+-----+ | 1 | 2 | 3 | xxx | +---+---+-----+-----+ | 6 | 7 | 13 | yyy | +---+---+-----+-----+
Or maybe I should do it another way?
source share