Results 1 to 4 of 4

Thread: Reimplement data() in QSqlQueryModel

  1. #1
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Reimplement data() in QSqlQueryModel

    Hi!

    I am experimenting with a subclass of QSqlQueryModel. Since I wanted to change the background color in the ListView, I reimplemented the data method

    Qt Code:
    1. QVariant BrowseWoTableModel::data( const QModelIndex &item, int role ) const
    2. {
    3. if( role == Qt::BackgroundColorRole )
    4. {
    5. return Qt::green;
    6. }
    7.  
    8. else if( role == Qt::DisplayRole )
    9. {
    10. return record( item.row() ).value( item.column() );
    11. }
    12.  
    13. return QVariant();
    14. }
    To copy to clipboard, switch view to plain text mode 

    This works for setting the background color, but fails in displaying the data.

    What am I missing here?

    //John

  2. #2
    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: Reimplement data() in QSqlQueryModel

    Your override returns null QVariants for roles like Qt::FontRole, Qt::TextColorRole and Qt::ForegroundRole.

    Try something like this:
    Qt Code:
    1. QVariant BrowseWoTableModel::data( const QModelIndex &item, int role ) const
    2. {
    3. if (!item.isValid())
    4. return QVariant();
    5.  
    6. if( role == Qt::BackgroundColorRole )
    7. return Qt::green;
    8. else
    9. return QSqlQueryModel::data(item, role);
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reimplement data() in QSqlQueryModel

    The "similar threads" box held the anwser.

    Qt Code:
    1. QVariant BrowseWoTableModel::data( const QModelIndex &item, int role ) const
    2. {
    3. QVariant ret = QSqlQueryModel::data( item, role );
    4.  
    5. if( role == Qt::BackgroundColorRole )
    6. {
    7. return Qt::green;
    8. }
    9.  
    10. return ret;
    11. }
    To copy to clipboard, switch view to plain text mode 

    This works. But i dont understand why. Where does the data come from?

  4. #4
    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: Reimplement data() in QSqlQueryModel

    Line 3 gathers the result you would received from a base QSqlQueryModel and that is returned unless the role is BackgroundColorRole.

Similar Threads

  1. QSqlQueryModel data update
    By psi in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2012, 03:59
  2. Keep data in QSqlQueryModel when server is offline
    By Tottish in forum Qt Programming
    Replies: 10
    Last Post: 28th April 2011, 09:14
  3. a faster QSqlQueryModel::data()
    By baray98 in forum Qt Programming
    Replies: 0
    Last Post: 24th September 2009, 00:56
  4. QTableView and reimplement data
    By estanisgeyer in forum Qt Programming
    Replies: 6
    Last Post: 7th May 2008, 18:18
  5. Different data types in a QSqlQueryModel
    By Lodas in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2008, 16:31

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.