Results 1 to 12 of 12

Thread: Color the rows in QTableView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    18
    Thanked 3 Times in 3 Posts

    Default Re: Color the rows in QTableView

    Hi, subclass the your Model (QSqlQuery for example) and reimplement virtual method data, like this:

    Qt Code:
    1. QVariant MyModelSqlQryModel::data(const QModelIndex &idx, int role) const
    2. {
    3.  
    4. QVariant v = QSqlQueryModel::data(idx, role);
    5.  
    6. if (role == Qt::BackgroundColor)
    7. {
    8. return QVariant(QColor(Qt::yellow));
    9. }
    10.  
    11. return (v);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Marcelo E. Geyer

  2. The following user says thank you to estanisgeyer for this useful post:

    grub87 (17th June 2009)

  3. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: Color the rows in QTableView

    here is a compilable example
    Qt Code:
    1. #include <QtGui>
    2. #include <QApplication>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. QTableWidget table(10, 10);
    9. QAbstractItemModel *model = table.model();
    10. for (int row = 0; row < table.rowCount(); ++row) {
    11. for (int column = 0; column < table.columnCount(); ++column) {
    12. QTableWidgetItem *newItem = new QTableWidgetItem(QObject::tr("%1").arg((row+1)*(column+1)));
    13. table.setItem(row, column, newItem);
    14. const QModelIndex index = model->index(row, column);
    15. model->setData(index, QColor(qrand()%255, qrand()%255, qrand()%255), Qt::BackgroundRole);
    16. }
    17. }
    18. table.show();
    19.  
    20. return app.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 12:49
  3. Count the number of rows in a QTableView
    By grub87 in forum Qt Programming
    Replies: 9
    Last Post: 28th June 2009, 16:31
  4. QTableView has constant number of rows
    By tomeks in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2008, 15:29
  5. QTableView number of rows and scrollbar issue
    By jnk5y in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 06:55

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
  •  
Qt is a trademark of The Qt Company.