Results 1 to 4 of 4

Thread: row colors

  1. #1
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default row colors

    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!

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: row colors

    Quote Originally Posted by nategoofs View Post
    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?
    Perhaps "m_pTable->setForegroundColor(bgc)"?

    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?
    Take a look at QAbstractItemView::alternatingRowColors docs. QPalette::Base and QPalette::AlternateBase are used for alternating row colors. Now, providing other color for Qt::BackgroundRole overwrites alternating row colors.
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    nategoofs (17th August 2007)

  4. #3
    Join Date
    Aug 2007
    Posts
    19
    Thanks
    5
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: row colors

    Thanks for the help on the foregroundcolor issue! Sometimes I guess you just need another pair of eyes .

    However, I did have a question regarding the QPalette::Base and AlternateBase. How would you set it up so that when they are needed, they return the correct values? For example:
    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 == QPalette::Base)
    15. return backgroundColor;
    16.  
    17. if (role == Qt::ForegroundRole )
    18. {
    19. return foregroundColor;
    20. }
    21. else if (role == Qt::DisplayRole )
    22. {
    23. switch(index.column())
    24. {
    25. case 0: return m_pTrackInfo->getScoreStr();
    26. case 1: return m_pTrackInfo->getTitle();
    27. case 2: return m_pTrackInfo->getArtist();
    28. case 3: return m_pTrackInfo->getType();
    29. case 4: return m_pTrackInfo->getDurationStr();
    30. case 5: return m_pTrackInfo->getBitrateStr();
    31. case 6: return m_pTrackInfo->getBpmStr();
    32. case 7: return m_pTrackInfo->getComment();
    33. }
    34. }
    35.  
    36. else
    37. return QVariant();
    38. }
    To copy to clipboard, switch view to plain text mode 
    "if(role == QPalette::Base)" does not work nor does "if(role == Qt::Base)". How then should I fix this for Base and AlternatingBase?

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: row colors

    Palette is a property of widget, see QWidget::palette for more details.
    Qt Code:
    1. QPalette palette = view->palette();
    2. palette.setColor(QPalette::Base, Qt::red);
    3. palette.setColor(QPalette::AltgernateBase, Qt::green);
    4. view->setPalette(palette);
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

Similar Threads

  1. QToolBox Colors
    By rooney in forum Qt Programming
    Replies: 3
    Last Post: 3rd September 2010, 14:37
  2. Fonts, Colors, and QStyle
    By Jimmy2775 in forum Qt Programming
    Replies: 1
    Last Post: 28th September 2006, 07:26
  3. Changing Progress Bar Colors
    By bpetty in forum Newbie
    Replies: 1
    Last Post: 11th August 2006, 17:29
  4. Qt Designer 4.0.1 Preview Colors
    By jtyler in forum Qt Tools
    Replies: 2
    Last Post: 14th February 2006, 15:47
  5. Draw a rectangle alternating two colors with qPainter
    By SkripT in forum Qt Programming
    Replies: 12
    Last Post: 24th January 2006, 23:12

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.