[SOLVED] Subclass QAbstractTableModel to handle SQLite tables
-
wrote on 2 Oct 2014, 16:05 last edited by
Hi,
First time posting so bare with me. I have already created QAbstractTableModel and QStyledItemDelegate classes and am getting better with model/view programming. I am working with PyQt4. How do I connect my custom model to handle SQLite tables now? I know there is QSqlTableModel, but I have already spent a considerable amount of time creating my own custom table model. How can I use my model with SQLite tables? Also how is the best way to reuse a model for different views? I have been creating a new model for every view which doesn't seem like the best approach. What do I need to add to support foreign key support to my custom table model? I want to be able to insert a record into a database table from my model and delete a record from the database. What other methods do I need?
QAbstactTableModel methods include:
flags
rowCount
columnCount
headerData
insertRows
data
setDataQStyledItemDelegate methods inculde:
createEditor
commitAndCloseEditor
setEditorData
setModelData
sizeHintThank you in advance.
Luke -
Hi and welcome to devnet,
If you don't want to use QSqlTableModel you'll have to use QSqlQuery. QSqlDatabase to connect to your database.
As for the multiple views with your model, just re-use the model.
@
MyModel *model = new MyModel;
QTableView *tableView = new QTableView;
tableView.setModel(model);
QListView *listView = new QListView;
listView.setModel(model)
// etc…
@Hope it helps
-
wrote on 3 Oct 2014, 14:32 last edited by
Thank you SGaist for your help. I do have one more question. I want to have a table view on the left and a tableview on the right. My goal is to show all the rows of the database on the left tableview. On the right tableview I want to have a long form. Right now I achieve this goal by having two models. Ideally if they select a row in the left model it updates the form model on the right? Rather then use two models for this would you use QDataMapper for this? I want the user to be able to edit either place and have the information updated in the database.
-
QDataWidgetMapper sounds better if you are creating a widget to edit your database fields
-
wrote on 15 Oct 2014, 14:58 last edited by
That is what I thought and thank you again SGaist.
-
You're welcome !
If that answers your question, please update the thread title prepending [solved] so other forum users may know as solution has been found :)
-
wrote on 16 Oct 2014, 14:18 last edited by
Hi SGaist,
Without sounding like a complete noob how do I add tag for solved?
Thank you,
Luke Kaim -
That's not noob at all, that thing is a bit hidden.
Just edit your first post and update the title
-
wrote on 16 Oct 2014, 14:49 last edited by
And I was thinking there would be a button for this. lol. I updated the title.
-
It's been asked and it's a known demand. Maybe for the next version of the forum :)