I have a QTreeview with 2 columns. One column there is supposed to be a pixmap and the other column has pixmap and icon. The column with just the pixmap does not show anything.
-
I have a pixmapList that holds my visibility icons. I am trying to add the visibility icon just for column = 1. Everything populates correctly for column=0
this->VisiblePixmapList = new QPixmap[(int)(VisibleIconType::LAST) + 1]; this->VisiblePixmapList[(int)VisibleIconType::EYEBALL].load(":/images/Eyeball.png"); this->VisiblePixmapList[(int)VisibleIconType::EYEBALL_GRAY].load(":/images/EyeballClosed.png"); QPixmap emptyPixmap(16,16); emptyPixmap.fill(); this->VisiblePixmapList[(int)VisibleIconType::LAST] = emptyPixmap;
QVariant function::data(const QModelIndex& idx, int role) const { if (!idx.isValid() || idx.model() != this) { return QVariant(); } auto item = reinterpret_cast<functionItem*>(idx.internalPointer()); auto source = item->Node; switch (role) { case Qt::ToolTipRole: if (source) { return QVariant(source->getToolTips().c_str()); } // THE BELOW DOES NOT WORK LIKE I INTEND case Qt::DisplayRole: if (idx.column() == 1) { auto test = this->VisiblePixmapList[(int)item->VisibilityIcon]; auto testSize = test.size(); auto test2 = test.scaled(QSize(32,32)); return QVariant(test2); } case Qt::EditRole: if (idx.column() == 0) { if (source) { return QVariant(source->getName().c_str()); } else { qDebug() << "Cannot decide type."; } } break; case Qt::DecorationRole: if (idx.column() == 0 && this->PixmapList) { if (item && item->getType() != function::Invalid) { auto scaledPixMap = this->PixmapList[(int)item->getIconType()].scaled(QSize(32,32)); return QVariant(scaledPixMap); } } break; } return QVariant(); }
-
-
Start by demonstrating what value
this->VisiblePixmapList[(int)item->VisibilityIcon]
has. It's probably correct, but we/you don't know that for sure till you print it out. -
Use
break
statements correctly and consistently in yourswitch
statement. -
You are returning a
QPixmap
forDisplayRole
. Where does Qt say you can do that?DisplayRole
has to resolve to a string, https://doc.qt.io/qt-6/qt.html#ItemDataRole-enum
Qt::DisplayRole
0 The key data to be rendered in the form of text. (QString
)QPixmap
does not, so probably comes out empty. I have not tried it, but from that page don't you want:Qt::DecorationRole
1 The data to be rendered as a decoration in the form of an icon. (QColor
,QIcon
orQPixmap
)?
-