Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
[SOLVED] Subclass QAbstractTableModel to handle SQLite tables
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:
QStyledItemDelegate methods inculde:
Thank you in advance.
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;
QListView *listView = new QListView;
Hope it helps
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
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 :)
Without sounding like a complete noob how do I add tag for solved?
That's not noob at all, that thing is a bit hidden.
Just edit your first post and update the title
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 :)