Hy all,
i've this class to change rows color of cells in table according their background.
I use this class in a slot from another class (main program qwidget) called every time a user clicks on a rows of a table
in the slot i've this simple code :
ui.mytable->setItemDelegate(new MyItemDelegate(this, CurrSelRow));
ui.mytable->setItemDelegate(new MyItemDelegate(this, CurrSelRow));
To copy to clipboard, switch view to plain text mode
All works well, and the cells' background is changed according to paint method.
but i'm wondering :
Every time the user clicks on a row, the slot is invoked and a new delegate is instanced.
And to destroy ??
I think a lot of memory is used if the user clicks many times on a row...
Is this right ??
Is there a way to destroy the istance ??
{
private:
int myRow;
public:
{
myRow = rowitem;
}
void paint
(QPainter* pPainter,
const QStyleOptionViewItem
& rOption,
const QModelIndex
& rIndex
) const {
if(rIndex.isValid())
{
QColor ItemBackgroundColor
= rIndex.
data(Qt
::BackgroundRole).
value<QColor>
();
QString ColorName
= ItemBackgroundColor.
name();
if (qVariantCanConvert<QString>(rIndex.data()))
val = qVariantValue<QString>(rIndex.data());
if (ItemBackgroundColor.isValid())
{
if(myRow > 0)
{
if (rIndex.row() == myRow)
{
if (ItemBackgroundColor == ClassColor::getRed())
{
ViewOption.
palette.
setColor(QPalette::HighlightedText, ClassColor
::getRed());
}
}
}
}
}
}
};
class MyItemDelegate: public QItemDelegate
{
private:
int myRow;
public:
MyItemDelegate(QObject* pParent = 0, int rowitem = 0) : QItemDelegate(pParent)
{
myRow = rowitem;
}
void paint(QPainter* pPainter, const QStyleOptionViewItem& rOption, const QModelIndex& rIndex) const
{
if(rIndex.isValid())
{
QStyleOptionViewItem ViewOption(rOption);
QColor ItemBackgroundColor = rIndex.data(Qt::BackgroundRole).value<QColor>();
QString ColorName = ItemBackgroundColor.name();
QString val;
if (qVariantCanConvert<QString>(rIndex.data()))
val = qVariantValue<QString>(rIndex.data());
if (ItemBackgroundColor.isValid())
{
if(myRow > 0)
{
if (rIndex.row() == myRow)
{
if (ItemBackgroundColor == ClassColor::getRed())
{
ViewOption.palette.setColor(QPalette::HighlightedText, ClassColor::getRed());
}
}
}
}
QItemDelegate::paint(pPainter, ViewOption, rIndex);
}
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks