PDA

View Full Version : SQL Model(s)?



jazzyeagle
21st May 2013, 15:49
I am working on a project in which I connect to a SQLite Database. I can already connect and access the data using the QSqlTableModel and a QTableView just fine. The issue I'm having with this class, however, is that it is set up for read/write at all times. I was hoping someone could give some general advice on the best way to set the structure up to do what I'm hoping to do.

Here's the basics: I want to have the main table set up to be read-only. This is the common user-interface, and they should not have direct access to change any of the data within the main QTableView. I want to have a plug-in to the program that allows maintainers of the database to make changes using pre-defined Dialogs using a QDataWidgetMapper. I initially was planning to use the same model (just a pointer to a QSqlTableModel variable), but I'm not sure if the one model can be read/write for one set of views but read-only for another set of views. I was looking through the Model Roles, and it would seem the EditRole would be available for all by default.

So, my questions are:

1) Can I have one model be both read-only & read/write dependent upon which view is calling it?

2) Can I subclass QSqlTableModel? I would worry in this scenario of updating the data() method, because of its connection with the SQL Database (I'm thinking I would have to reimplement manually all of the benefits of using the QSqlTableModel?)

3) If you were trying to implement this, what steps would you take?

I ask all of this in theory, but if you feel you need to take a look at the project for any reason to help answer the questions, it's an open source project that you can find at https://github.com/jazzyeagle/LCSI/tree/PhaseTwo. Mostly, it's just the dialog boxes that have been created (I haven't implemented the plug-in stuff yet). Thank you!!

Rhayader
21st May 2013, 19:08
Just a quick answer for your first question. Setting the EditTriggers of the view to NoEditTriggers qabstractitemview.html#editTriggers-prop the view becomes read only.

Santosh Reddy
23rd May 2013, 14:31
1) Can I have one model be both read-only & read/write dependent upon which view is calling it?
Yes, as the other post suggests, set the QAbstractItemView::NoEditTriggers on the read-only view


2) Can I subclass QSqlTableModel? I would worry in this scenario of updating the data() method, because of its connection with the SQL Database (I'm thinking I would have to reimplement manually all of the benefits of using the QSqlTableModel?)
Subclass is not required here. You could also use QSqlQueryModel for ready only model, and QSqlTableModel for read/write model. Note that QSqlQueryModel class provides a read-only data model for SQL result sets.


3) If you were trying to implement this, what steps would you take?
It's simple and quick to use QAbstractItemView::NoEditTriggers on view.