PDA

View Full Version : [SOLVED] Get QModelIndex data



juliano.gomes
20th November 2015, 18:59
Greetings!

Currently I have the class below, which paints the lines of column 94 of a QTableView.

I need to get the values of each line of this column, to check which color it will paint in that row/column



void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == 94)
{
if (GetValueOfColumn94 <= 20)
painter->fillRect(option.rect, Qt::green);
else if (GetValueOfColumn94 >=21<=60)
painter->fillRect(option.rect, Qt::yellow);
if (GetValueOfColumn94 >=61<=100)
painter->fillRect(option.rect, Qt::red);
}

return QItemDelegate::paint(painter, option, index);
}



in code above, the "GetValueOfColumn94" is what i need. I've tried with this 2 methods below, but not work



index.sibling(index.row(),94).data().toInt();
index.model()->data(index.model()->index(index.row(),94),Qt::DisplayRole).toInt();


someone could help me?
Thanks!
Juliano

Greetings!

Currently I have the class below, which paints the lines of column 94 of a QTableView.

I need to get the values of each line of this column, to check which color it will paint in that row/column



void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == 94)
{
if (GetValueOfColumn94 <= 20)
painter->fillRect(option.rect, Qt::green);
else if (GetValueOfColumn94 >=21<=60)
painter->fillRect(option.rect, Qt::yellow);
if (GetValueOfColumn94 >=61<=100)
painter->fillRect(option.rect, Qt::red);
}

return QItemDelegate::paint(painter, option, index);
}



in code above, the "GetValueOfColumn94" is what i need. I've tried with this 2 methods below, but not work



index.sibling(index.row(),94).data().toInt();
index.model()->data(index.model()->index(index.row(),94),Qt::DisplayRole).toInt();


someone could help me?
Thanks!
Juliano

Added after 1 27 minutes:

Solved!




QVariant GetValueOfColumn94 = index.model()->data(index.model()->index(index.row(),94),Qt::DisplayRole);

if (GetValueOfColumn94 == "Green")
painter->fillRect(option.rect, QColor(170, 216, 0));
else if (GetValueOfColumn94 == "Yellow")
painter->fillRect(option.rect, QColor(254, 251, 24));
if (GetValueOfColumn94 == "Red")
painter->fillRect(option.rect, QColor(255, 40, 0));


Thanks!
Juliano

anda_skoa
21st November 2015, 16:10
Or just


index.data()


Cheers,
_

juliano.gomes
23rd November 2015, 16:30
Or just


index.data()


Cheers,
_

Yes anda_skoa, much more simple than i've thought.

changing the subject a little, if I need to paint a fraction of the cell, instead of painting it all, as in the last column of the picture attached, could I do this this without a lot of work?
11534

hugs!
Juliano

Yes anda_skoa, much more simple than i've thought.

changing the subject a little, if I need to paint a fraction of the cell, instead of painting it all, as in the last column of the picture attached, could I do this this without a lot of work?
11535

hugs!
Juliano

anda_skoa
24th November 2015, 07:47
You can do that with a custom delegate.
E.g. derive from QStyledItemDelegate, implement paint() such that it calls the base class for the usual painting and paints your customization either before or after that.

Cheers,
_