Ah, so that's what I was looking for, Qt::UserRole does the job for me.
Well, I solved it now with QTableWidget + subclass of QTableWidgetItem. In this subclass, I added a member that holds all data that has to be stored into the cell and also I reimplemented the setData method, so by calling setData() now, the member will be updated and set. And see, the table is immediatelly updated and repainted. Really great!
Now I recognized one more (little) problem. In my reimplemented paint function, I do text drawing with different text colors and different thickness. For this purpose, I create different QPens, like this:
{
QStyledItemDelegate::paint(pPainter, soviOption, miIndex);
myTableCellData cellData = this->myTable->item(miIndex.row(), miIndex.column())->data(Qt::UserRole).value<myTableCellData>();
QPen pPen
(pPainter
->pen
());
QRect rectStringDay
= soviOption.
rect, rectStringEvent
= soviOption.
rect;
rectStringEvent.setX(rectStringEvent.x() + 10);
pPainter->fillRect(soviOption.rect, cellData.colorBackground);
pPen.setColor(cellData.colorStringDay);
pPen.setWidth(10); // <--- doesn't work
pPainter->setPen(pPen);
pPainter->drawText(rectStringDay, cellData.stringDay);
pPen.setColor(cellData.colorStringEvents);
pPen.setWidth(2); // <--- doesn't work
pPainter->setPen(pPen);
for(int i = 0; i < 3; i++)
{
if(i >= cellData.slEvents.count())
break;
rectStringEvent.setY(soviOption.rect.y() + 3 + (i + 1) * 12);
pPainter->drawText(rectStringEvent, cellData.slEvents.at(i));
}
return;
}
void myItemDelegate::paint(QPainter *pPainter, const QStyleOptionViewItem &soviOption, const QModelIndex &miIndex) const
{
QStyledItemDelegate::paint(pPainter, soviOption, miIndex);
myTableCellData cellData = this->myTable->item(miIndex.row(), miIndex.column())->data(Qt::UserRole).value<myTableCellData>();
QPen pPen(pPainter->pen());
QRect rectStringDay = soviOption.rect, rectStringEvent = soviOption.rect;
rectStringEvent.setX(rectStringEvent.x() + 10);
pPainter->fillRect(soviOption.rect, cellData.colorBackground);
pPen.setColor(cellData.colorStringDay);
pPen.setWidth(10); // <--- doesn't work
pPainter->setPen(pPen);
pPainter->drawText(rectStringDay, cellData.stringDay);
pPen.setColor(cellData.colorStringEvents);
pPen.setWidth(2); // <--- doesn't work
pPainter->setPen(pPen);
for(int i = 0; i < 3; i++)
{
if(i >= cellData.slEvents.count())
break;
rectStringEvent.setY(soviOption.rect.y() + 3 + (i + 1) * 12);
pPainter->drawText(rectStringEvent, cellData.slEvents.at(i));
}
return;
}
To copy to clipboard, switch view to plain text mode
Setting the different text colors always works, but changing the pen's thickness with QPen::setWidth(int) doesn't change anything. Do you know why? I also tried to manually set the cosmetic mode to false, but it still doesn't work.
Any suggestions?
Bookmarks