QTableView column trouble
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:
Code:
Qt
::ItemFlags WTrackTableModel
::flags(const QModelIndex &index
) const{
if (!index.isValid())
return Qt::ItemIsEnabled;
switch(index.column())
{
}
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:
Code:
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
if (!WWidget::selectNode(node, "FgColor").isNull())
{
fgc.setNamedColor(WWidget::selectNodeQString(node, "FgColor"));
}
m_pTable->setForegroundColor(fgc);
// Row colors
if (!WWidget::selectNode(node, "BgColorRowEven").isNull())
{
r1.setNamedColor(WWidget::selectNodeQString(node, "BgColorRowEven"));
r2.setNamedColor(WWidget::selectNodeQString(node, "BgColorRowUneven"));
setAlternatingRowColors ( true );
m_pTable->setRowColor(r1, r2);
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:
Code:
{
TrackInfoObject *m_pTrackInfo = m_pTrackPlaylist->getTrackAt(index.row());
if (!index.isValid())
if (index.row() >= m_pTrackPlaylist->getSongNum())
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
}
Thanks!
Nate
Re: QTableView column trouble
Quote:
Originally Posted by
nategoofs
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"?
Quote:
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.
Re: QTableView column trouble
Quote:
Did you try changing the selectionBehavior of the view to "SelectRows"?
shouldn't setSelectionBehavior(QAbstractItemView::SelectRows ); work? I have that set up right now.
Quote:
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?
Re: QTableView column trouble
Quote:
Originally Posted by
nategoofs
shouldn't setSelectionBehavior(QAbstractItemView::SelectRows ); work? I have that set up right now.
Yes, that's exactly what I meant.
Quote:
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().
Re: QTableView column trouble
Quote:
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.
Re: QTableView column trouble
Could you prepare a minimal compilable example reproducing the problem?
Re: QTableView column trouble
return return Qt::ItemIsEnabled|Qt::ItemIsSelectable; to have the other items selectable as well