@SeDi For posterity, here's my working solution:
delegate: Rectangle {
width: childrenRect.width
height: childrenRect.height
Loader {
sourceComponent: {
switch(columnNo) {
case 0: return firstDelegate;
case 1: return secondDelegate;
// ...
default: return etcDelegate;
}
}
}
Component {
id: firstDelegate
Rectangle {
width: childrenRect.width
height: childrenRect.height
color: "lightgray"
Text {
width: contentWidth + contentHeight
height: contentHeight * 1.1
text: "first"
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
}
}
Component {
id: secondDelegate
Rectangle {
width: childrenRect.width
height: childrenRect.height
color: "yellow"
Text {
width: contentWidth + contentHeight
height: contentHeight * 1.1
text: "second"
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
}
}
Component {
id: etcDelegate
Rectangle {
width: childrenRect.width
height: childrenRect.height
color: "lightgreen"
Text {
width: contentWidth + contentHeight
height: contentHeight * 1.1
text: foodName
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter
}
}
}
}
To provide the columnNo role I had to do some work in my QAbstractTableModel.
The roles enum in the header:
class LogTableModel : public QAbstractTableModel
{
enum Role {
DisplayRole = Qt::DisplayRole,
DecorationRole = Qt::DecorationRole,
StatusTipRole = Qt::StatusTipRole,
//whatever...
ColumnNoRole = Qt::UserRole+5 // or wherever it suits
...
In data(...) I have implemented:
QVariant LogTableModel::data(const QModelIndex &index, int role) const
{
if (index.isValid()) {
switch(role)
{
case DisplayRole: return QVariant::fromValue(m_columns.at(index.column())->at(index.row()));
case ColumnNoRole: return QVariant::fromValue(index.column());
...
roleNames() must also know it:
QHash<int, QByteArray> LogTableModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[DisplayRole] = "display";
roles[ColumnNoRole] = "columnNo";
...