Im using a reimplemented QAbstractTableModel and a QTableView. What Im doing is getting information about how the model/view should look from an xml file (background color, foreground color etc.)
This is from my view...
Qt Code:
  1. void WTrackTableView::setup(QDomNode node)
  2. {
  3. // Position
  4. if (!WWidget::selectNode(node, "Pos").isNull())
  5. {
  6. QString pos = WWidget::selectNodeQString(node, "Pos");
  7. int x = pos.left(pos.find(",")).toInt();
  8. int y = pos.mid(pos.find(",")+1).toInt();
  9. move(x,y);
  10. }
  11.  
  12. // Size
  13. if (!WWidget::selectNode(node, "Size").isNull())
  14. {
  15. QString size = WWidget::selectNodeQString(node, "Size");
  16. int x = size.left(size.find(",")).toInt();
  17. int y = size.mid(size.find(",")+1).toInt();
  18. setFixedSize(x,y);
  19. }
  20.  
  21. // Background color
  22. QColor bgc(255,255,255);
  23. if (!WWidget::selectNode(node, "BgColor").isNull())
  24. {
  25. bgc.setNamedColor(WWidget::selectNodeQString(node, "BgColor"));
  26. }
  27. m_pTable->setBackgroundColor(bgc);
  28.  
  29. // Foreground color
  30. QColor fgc(0,0,0);
  31. if (!WWidget::selectNode(node, "FgColor").isNull())
  32. {
  33. fgc.setNamedColor(WWidget::selectNodeQString(node, "FgColor"));
  34. }
  35. m_pTable->setForegroundColor(bgc);
  36. /*
  37.   // Row colors
  38.   if (!WWidget::selectNode(node, "BgColorRowEven").isNull())
  39.   {
  40.   QColor r1;
  41.   r1.setNamedColor(WWidget::selectNodeQString(node, "BgColorRowEven"));
  42.   QColor r2;
  43.   r2.setNamedColor(WWidget::selectNodeQString(node, "BgColorRowUneven"));
  44. setAlternatingRowColors ( true );
  45. m_pTable->setRowColor(r1, r2);
  46.   }
  47.  
  48.   // BPM confidence colors
  49.   if (!WWidget::selectNode(node, "BgColorBpmNoConfirm").isNull())
  50.   {
  51.   QColor c1;
  52.   c1.setNamedColor(WWidget::selectNodeQString(node, "BgColorBpmNoConfirm"));
  53.   QColor c2;
  54.   c2.setNamedColor(WWidget::selectNodeQString(node, "BgColorBpmConfirm"));
  55. WTrackTableItem::setBpmBgColors(WSkinColor::getCorrectColor(c1),
  56. WSkinColor::getCorrectColor(c2));
  57.   }
  58. */
  59. }
To copy to clipboard, switch view to plain text mode 

Here are the functions in the model:
Qt Code:
  1. void WTrackTableModel::setBackgroundColor(QColor bgColor)
  2. {
  3. backgroundColor = bgColor;
  4. }
  5. void WTrackTableModel::setForegroundColor(QColor fgColor)
  6. {
  7. foregroundColor = fgColor;
  8. }
  9. void WTrackTableModel::setRowColor(QColor evenColor, QColor unevenColor)
  10. {
  11. rowEvenColor = evenColor;
  12. rowUnevenColor = unevenColor;
  13. }
  14. void WTrackTableModel::setBpmColor(QColor confirmColor, QColor noConfirmColor)
  15. {
  16. bpmNoConfirmColor = noConfirmColor;
  17. bpmConfirmColor = confirmColor;
  18. }
To copy to clipboard, switch view to plain text mode 

And Finally, the data function:
Qt Code:
  1. QVariant WTrackTableModel :: data(const QModelIndex &index, int role) const
  2. {
  3. TrackInfoObject *m_pTrackInfo = m_pTrackCollection->getTrack(index.row()+1);
  4. if (!index.isValid())
  5. return QVariant();
  6.  
  7. if (index.row() >= m_pTrackCollection->getSize())
  8. return QVariant();
  9.  
  10. if (role == Qt::BackgroundRole)
  11. {
  12. return backgroundColor;
  13. }
  14. if (role == Qt::ForegroundRole )
  15. {
  16. return foregroundColor;
  17. }
  18. else if (role == Qt::DisplayRole )
  19. {
  20. switch(index.column())
  21. {
  22. case 0: return m_pTrackInfo->getScoreStr();
  23. case 1: return m_pTrackInfo->getTitle();
  24. case 2: return m_pTrackInfo->getArtist();
  25. case 3: return m_pTrackInfo->getType();
  26. case 4: return m_pTrackInfo->getDurationStr();
  27. case 5: return m_pTrackInfo->getBitrateStr();
  28. case 6: return m_pTrackInfo->getBpmStr();
  29. case 7: return m_pTrackInfo->getComment();
  30. }
  31. }
  32.  
  33. else
  34. return QVariant();
  35. }
To copy to clipboard, switch view to plain text mode 

Now my problem is that the foreground color does not come out as I want it to. It compiles fine, but when I go to look at my model/view at first glance it appears to be an empty table. but when I click around, the Items in the model show up fine when they're highlighted. What could be causing this?

Also, I'm having some trouble figuring out how to set up alternating colors in my model. as you can see setAlternatingRowColors(true) is used but i dont feel that it is working. What do I need to do to these functions to get them to set the row coloring?

Thanks!