PDA

View Full Version : row colors



nategoofs
17th August 2007, 03:01
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...


void WTrackTableView::setup(QDomNode node)
{
// Position
if (!WWidget::selectNode(node, "Pos").isNull())
{
QString pos = WWidget::selectNodeQString(node, "Pos");
int x = pos.left(pos.find(",")).toInt();
int y = pos.mid(pos.find(",")+1).toInt();
move(x,y);
}

// Size
if (!WWidget::selectNode(node, "Size").isNull())
{
QString size = WWidget::selectNodeQString(node, "Size");
int x = size.left(size.find(",")).toInt();
int y = size.mid(size.find(",")+1).toInt();
setFixedSize(x,y);
}

// Background color
QColor bgc(255,255,255);
if (!WWidget::selectNode(node, "BgColor").isNull())
{
bgc.setNamedColor(WWidget::selectNodeQString(node, "BgColor"));
}
m_pTable->setBackgroundColor(bgc);

// Foreground color
QColor fgc(0,0,0);
if (!WWidget::selectNode(node, "FgColor").isNull())
{
fgc.setNamedColor(WWidget::selectNodeQString(node, "FgColor"));
}
m_pTable->setForegroundColor(bgc);
/*
// Row colors
if (!WWidget::selectNode(node, "BgColorRowEven").isNull())
{
QColor r1;
r1.setNamedColor(WWidget::selectNodeQString(node, "BgColorRowEven"));
QColor r2;
r2.setNamedColor(WWidget::selectNodeQString(node, "BgColorRowUneven"));
setAlternatingRowColors ( true );
m_pTable->setRowColor(r1, r2);
}

// BPM confidence colors
if (!WWidget::selectNode(node, "BgColorBpmNoConfirm").isNull())
{
QColor c1;
c1.setNamedColor(WWidget::selectNodeQString(node, "BgColorBpmNoConfirm"));
QColor c2;
c2.setNamedColor(WWidget::selectNodeQString(node, "BgColorBpmConfirm"));
WTrackTableItem::setBpmBgColors(WSkinColor::getCor rectColor(c1),
WSkinColor::getCorrectColor(c2));
}
*/
}


Here are the functions in the model:


void WTrackTableModel::setBackgroundColor(QColor bgColor)
{
backgroundColor = bgColor;
}
void WTrackTableModel::setForegroundColor(QColor fgColor)
{
foregroundColor = fgColor;
}
void WTrackTableModel::setRowColor(QColor evenColor, QColor unevenColor)
{
rowEvenColor = evenColor;
rowUnevenColor = unevenColor;
}
void WTrackTableModel::setBpmColor(QColor confirmColor, QColor noConfirmColor)
{
bpmNoConfirmColor = noConfirmColor;
bpmConfirmColor = confirmColor;
}


And Finally, the data function:


QVariant WTrackTableModel :: data(const QModelIndex &index, int role) const
{
TrackInfoObject *m_pTrackInfo = m_pTrackCollection->getTrack(index.row()+1);
if (!index.isValid())
return QVariant();

if (index.row() >= m_pTrackCollection->getSize())
return QVariant();

if (role == Qt::BackgroundRole)
{
return backgroundColor;
}
if (role == Qt::ForegroundRole )
{
return foregroundColor;
}
else if (role == Qt::DisplayRole )
{
switch(index.column())
{
case 0: return m_pTrackInfo->getScoreStr();
case 1: return m_pTrackInfo->getTitle();
case 2: return m_pTrackInfo->getArtist();
case 3: return m_pTrackInfo->getType();
case 4: return m_pTrackInfo->getDurationStr();
case 5: return m_pTrackInfo->getBitrateStr();
case 6: return m_pTrackInfo->getBpmStr();
case 7: return m_pTrackInfo->getComment();
}
}

else
return QVariant();
}


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!

jpn
17th August 2007, 09:35
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 (http://doc.trolltech.com/4.3/qabstractitemview.html#alternatingRowColors-prop) docs. QPalette::Base and QPalette::AlternateBase are used for alternating row colors. Now, providing other color for Qt::BackgroundRole overwrites alternating row colors.

nategoofs
17th August 2007, 18:41
Thanks for the help on the foregroundcolor issue! Sometimes I guess you just need another pair of eyes :p.

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:


QVariant WTrackTableModel :: data(const QModelIndex &index, int role) const
{
TrackInfoObject *m_pTrackInfo = m_pTrackCollection->getTrack(index.row()+1);
if (!index.isValid())
return QVariant();

if (index.row() >= m_pTrackCollection->getSize())
return QVariant();

/*if (role == Qt::BackgroundRole)
{
return backgroundColor;
}*/
if(role == QPalette::Base)
return backgroundColor;

if (role == Qt::ForegroundRole )
{
return foregroundColor;
}
else if (role == Qt::DisplayRole )
{
switch(index.column())
{
case 0: return m_pTrackInfo->getScoreStr();
case 1: return m_pTrackInfo->getTitle();
case 2: return m_pTrackInfo->getArtist();
case 3: return m_pTrackInfo->getType();
case 4: return m_pTrackInfo->getDurationStr();
case 5: return m_pTrackInfo->getBitrateStr();
case 6: return m_pTrackInfo->getBpmStr();
case 7: return m_pTrackInfo->getComment();
}
}

else
return QVariant();
}

"if(role == QPalette::Base)" does not work nor does "if(role == Qt::Base)". How then should I fix this for Base and AlternatingBase?

jpn
17th August 2007, 19:16
Palette is a property of widget, see QWidget::palette (http://doc.trolltech.com/4.3/qwidget.html#palette-prop) for more details.


QPalette palette = view->palette();
palette.setColor(QPalette::Base, Qt::red);
palette.setColor(QPalette::AltgernateBase, Qt::green);
view->setPalette(palette);