PDA

View Full Version : QItemDelegate and Qtablewidget : where to destroy pointer ???



gab74
27th September 2013, 17:38
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));

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 ??




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::HighlightedT ext, ClassColor::getRed());

}
}

}

}
QItemDelegate::paint(pPainter, ViewOption, rIndex);


}

}
};

anda_skoa
27th September 2013, 20:27
Why don't you just keep a pointer to the delegate and call a method that sets the new CurrSelRow value?

Cheers,
_

gab74
30th September 2013, 11:08
Yes i try in this way too...i keep a pointer and a use a set CurrSellRow method,
but i notice that when i click on a row...the colors change not immediatly...but after some seconds...
I dont' know how to call paint method on delegate immediatly...

Using instead a NEW for every click, works well and the colour change immediatly...so i think that when the new object is created the paint method is called immediatly...

Any helps ??
Thanks !!!

anda_skoa
30th September 2013, 22:13
Hmm, I see.

Have you tried if calling update() on the widget helps?

Cheers,
_

gab74
9th October 2013, 16:27
I used
a pointer initialized in 2 points of my software, and use a single delete for the pointer in a closing method.