Results 1 to 3 of 3

Thread: SQL Model(s)?

  1. #1
    Join Date
    Nov 2010
    Location
    Thornton, CO
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question SQL Model(s)?

    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!!

  2. #2
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQL Model(s)?

    Just a quick answer for your first question. Setting the EditTriggers of the view to NoEditTriggers http://doc.qt.io/qt-4.8/qabstractitemview.html#editTriggers-prop the view becomes read only.

  3. The following user says thank you to Rhayader for this useful post:

    jazzyeagle (21st May 2013)

  4. #3
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: SQL Model(s)?

    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.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 9
    Last Post: 14th February 2013, 20:39
  2. Event handlers for QTableView model / selection model.
    By hickscorp in forum Qt Programming
    Replies: 2
    Last Post: 8th July 2011, 18:57
  3. Using model indices in complex model item relationships
    By hackerNovitiate in forum Newbie
    Replies: 0
    Last Post: 29th June 2011, 15:30
  4. Replies: 1
    Last Post: 24th February 2011, 06:54
  5. Model/view, apply a filter on model
    By remy_david in forum Qt Programming
    Replies: 4
    Last Post: 4th February 2011, 18:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.