Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

What to subclass?



  • Hi,

    currently I'm subclassing QSqlQueryModel to get an editable model (data comes from SQLite DB).
    I select the "base"-data with a QSqlQuery at the moment, but now I need to insert columns manually, because I want to show data, which is calculated on runtime (not in DB).
    Somewhere I've read that I need to subclass QAbstractItemModel or QAbstractTableModel for this. But I think, I'm losing the Query functionality then, dont I?

    The next thing I'm thinking of, is using some widgets like QComboBox inside the model to select between given values, when editing.

    So basically:

    • Model shows data from DB (multiple tables), currently by executing a QSqlQuery
    • user clicks an editable cell (where "editable" flag was set), if cell contains digits (int type) -> ComboBox with valid numbers is shown,
    • user can select value, on "enter" or something like that, the comboBox value is written to the db / model,
    • the ComboBox disappears afterwards and the new value is shown inside the model.

    How to insert these values to my db, when I dont have queries anymore?

    I dont know if all these features are possible by sublassing one given Model-class or how else I can implement this.

    Any help or code examples appreciated :)

    EDIT:
    The view is a QTableView (tables are joined). I guess I have to change the view as well (to use comboBoxes for editing)?!


  • Qt Champions 2019

    @pl45m4 said in What to subclass?:

    when I dont have queries anymore?

    What prohibits you to use a QSqlQuery within your custom class derived from QAbstractItemModel?



  • @christian-ehrlicher

    I assumed that QAbstractItemModel provides no setQuery(). So I have to write my own function to execute a QSqlQuery?


  • Qt Champions 2019

    @pl45m4 said in What to subclass?:

    So I have to write my own function to execute a QSqlQuery?

    Yes



  • @christian-ehrlicher

    Is there any other way to get data from SQLite database with Qt except queries?
    Implementing the functionality to use SQL queries is not what I what to do. It sounds very complicated or is it not that hard?


  • Qt Champions 2019

    I don't see what's complicated executing a QSqlQuery and fetching the data. Especially compared to the idea to parse the data somehow from a sqlite file...



  • @christian-ehrlicher said in What to subclass?:

    I don't see what's complicated executing a QSqlQuery and fetching the data.

    Because I cannot add the calculated data to my model, if I use a QSqlTableModel subclass, so I thought I can subclass QAbstractTableModel or QAbstractItemModel but setQuery() is implemented first in QSqlTableModel.

    EDIT:
    You mean executing the query to get the data from DB, without setting the data to the model directly?
    So I can execute the query to get my "base"-data, then calculate the missing cols and then set the data to the model?


  • Qt Champions 2019

    @pl45m4 said in What to subclass?:

    You mean executing the query to get the data from DB, without setting the data to the model directly?

    Yes



  • @christian-ehrlicher said in What to subclass?:

    Especially compared to the idea to parse the data somehow from a sqlite file

    Where I have said that? :)
    I never had the idea to "parse" the sqlite file directly.

    I will try to get the data with a query first and see what I can do with it :)
    (I guess QSqlResult is the key?!)