Results 1 to 6 of 6

Thread: Understanding QAbstractTableModel::data() with QSqlQuery

Hybrid View

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

    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
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    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
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    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?

Similar Threads

  1. QAbstractTableModel not showing data
    By rspock in forum Qt Programming
    Replies: 2
    Last Post: 22nd March 2013, 23:30
  2. Replies: 1
    Last Post: 20th May 2009, 21:36
  3. QAbstractTableModel , custom data
    By akon in forum Newbie
    Replies: 0
    Last Post: 17th April 2009, 17:03
  4. QAbstractTableModel data insert issues
    By nategoofs in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 10:16
  5. QAbstractTableModel::data not being called...
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2007, 10: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
  •  
Qt is a trademark of The Qt Company.