Results 1 to 6 of 6

Thread: Understanding QAbstractTableModel::data() with QSqlQuery

  1. #1
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Understanding QAbstractTableModel::data() with QSqlQuery

    Hello,
    I'm fairly new to Qt and C++, and I'm having trouble understanding how exactly to populate a subclass of QAbstractTableModel with data from a QSqlQuery. Say for example I have a sql query that returns the itemID, description, and price for some number of items (data is from two joined tables, so QSqlTableModel will not work). Assume that I want to put the returned data in a QList<QSqlRecord> (as suggested in this post: http://www.qtcentre.org/threads/2114...from-QSqlQuery ) and also assume I have a subclass of QAbstractTableModel called MyTableModel. Could someone please give me a brief example of what the MyTableModel::data function might look like and how it would be implemented? Should I pass the QList to the constructor for MyTableModel, the data function itself, or none of the above? Any tips would be much appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Understanding QAbstractTableModel::data() with QSqlQuery

    You would probably have that QList as a member of the model.
    If you pass it in at the constructor or if you make the model itself to the query is your choice, the second is closer to what the Qt SQL models would do.

    In data you get the QModelIndex identifying the cell the view wants data for, so in your case the row() would be the index into the list and the column() which part of the record you have in that respective view column.
    The role argument tells you which type of data the view is interested in, in the simplest case you just react to Qt::DisplayRole and return the cell's data, otherwise you just return an default constructed QVariant.

    Cheers,
    _

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Understanding QAbstractTableModel::data() with QSqlQuery

    Before you spend a lot of time on this, is there a reason that QSqlQueryModel is not a good solution or starting point?

  4. #4
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Understanding QAbstractTableModel::data() with QSqlQuery

    Is data() automatically called at some point, or do I need to call it myself?

    I need to manipulate some of the data before I display it, as well as have an additional column containing data that is not returned by the sql query. Correct me if I am wrong, but my understanding was that neither of these things is possible with QSqlQueryModel?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Understanding QAbstractTableModel::data() with QSqlQuery

    You implement data(). Data() will be called by any view, proxy model, or consumer of the data in the model when it requires the content of the model.

    If you subclass QSqlQuery model you get most of the data handling for free. To add a derived column to the actual database columns, for example, you could:
    • Override columnCount() to return QSqlQueryModel::columnCount() plus one.
    • Override data() to return a derived values for the extra column, and QSqlQueryModel::data() for other columns.
    • Override headerData() in a similar way to return a decent heading.

    If you wanted particular values to render in red by default then your data() could return a different value when the Qt::ForegroundRole is requested for an index, and the base model value for all other roles. The permutations are numerous.

    You can also do this sort of manipulation with a proxy model.

  6. #6
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Understanding QAbstractTableModel::data() with QSqlQuery

    Okay, that's some great information to run with, thank you!

Similar Threads

  1. QAbstractTableModel not showing data
    By rspock in forum Qt Programming
    Replies: 2
    Last Post: 22nd March 2013, 22:30
  2. Replies: 1
    Last Post: 20th May 2009, 20:36
  3. QAbstractTableModel , custom data
    By akon in forum Newbie
    Replies: 0
    Last Post: 17th April 2009, 16:03
  4. QAbstractTableModel data insert issues
    By nategoofs in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 09:16
  5. QAbstractTableModel::data not being called...
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2007, 09:52

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.