PDA

View Full Version : QTableView column trouble



nategoofs
20th August 2007, 04:09
Im using a QAbstractTableModel with a QTableView and I would like to change a few things graphically.
1. I know how to make one specific column editable. However, when I select a row, the entire row does not highlight, only the editable column on that row is highlighted. How do I go about getting the entire row highlighted, but leaving the one column eligible for editing.

Here is my flags function:


Qt::ItemFlags WTrackTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
switch(index.column())
{
case 7: return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
return Qt::ItemIsEnabled;
}


2. I set my row colors according to the color taken from an xml file like so if row colors are present:


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);
}
// Foreground color
QColor fgc(0,0,0);
if (!WWidget::selectNode(node, "FgColor").isNull())
{
fgc.setNamedColor(WWidget::selectNodeQString(node, "FgColor"));
}
m_pTable->setForegroundColor(fgc);

// 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);
QPalette Rowpalette = palette();
Rowpalette.setColor(QPalette::Base, r1);
Rowpalette.setColor(QPalette::AlternateBase, r2);
setPalette(Rowpalette);
}

/*
// 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"));

}
*/
}

in the QTableView on lines 28-41. Now what I would like to do is use the colors found in the BPM if statement on items in one single column. Say I have a function [bool bpmConfirm()] that returns true if the BPM is confirmed, I would then like to use r2, and r1 is the BPM is not confirmed. How would I go about doing this?

just for reference, here is what my data function looks like in my model:


QVariant WTrackTableModel :: data(const QModelIndex &index, int role) const
{
TrackInfoObject *m_pTrackInfo = m_pTrackPlaylist->getTrackAt(index.row());

if (!index.isValid())
return QVariant();

if (index.row() >= m_pTrackPlaylist->getSongNum())
return QVariant();

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();
}

Thanks!
Nate

wysota
20th August 2007, 09:46
1. I know how to make one specific column editable. However, when I select a row, the entire row does not highlight, only the editable column on that row is highlighted. How do I go about getting the entire row highlighted, but leaving the one column eligible for editing.
Did you try changing the selectionBehavior of the view to "SelectRows"?


Now what I would like to do is use the colors found in the BPM if statement on items in one single column. Say I have a function [bool bpmConfirm()] that returns true if the BPM is confirmed, I would then like to use r2, and r1 is the BPM is not confirmed. How would I go about doing this?

Return those colors from the data() method of your model for the appropriate role (Foreground or Background) if you want that behaviour in the model or provide a custom delegate for the view and make the delegate "BPM aware" and simply paint using a different colour.

nategoofs
20th August 2007, 17:30
Did you try changing the selectionBehavior of the view to "SelectRows"?
shouldn't setSelectionBehavior(QAbstractItemView::SelectRows ); work? I have that set up right now.


Return those colors from the data() method of your model for the appropriate role (Foreground or Background) if you want that behaviour in the model or provide a custom delegate for the view and make the delegate "BPM aware" and simply paint using a different colour

How would I go about making a custom delegate for this?

wysota
20th August 2007, 18:21
shouldn't setSelectionBehavior(QAbstractItemView::SelectRows ); work? I have that set up right now.

Yes, that's exactly what I meant.


How would I go about making a custom delegate for this?
You can start by reading QAbstractItemDelegate docs and then subclassing QItemDelegate and reimplementing drawDisplay() or paint().

nategoofs
21st August 2007, 02:26
Yes, that's exactly what I meant.
But my problem is that I had setSelectionBehavior(QAbstractItemView::SelectRows ); set up already, and it's not working.

wysota
21st August 2007, 08:04
Could you prepare a minimal compilable example reproducing the problem?

mogaf
27th October 2009, 20:14
return return Qt::ItemIsEnabled|Qt::ItemIsSelectable; to have the other items selectable as well