Aslam
Aslam

Reputation: 751

QAbstractListModel returning unexpcted result on Qt::DisplayRole

I have created a very minimal QAbstractListModel with two roles, display and test

InvoiceTabModel::InvoiceTabModel(QObject *parent): QAbstractListModel(parent)
{

}

QVariant InvoiceTabModel::data(const QModelIndex &index, int role) const
{
    Q_UNUSED(index)
    if(role == 123)
        return QVariant("testRole");
    return QVariant("displayRole");
}

int InvoiceTabModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return 3;
}

QHash<int, QByteArray> InvoiceTabModel::roleNames() const
{
    return { {Qt::DisplayRole, "display"}, {123, "test"} };
}

I attached this model to a repeater

Repeater{
    id: invoiceTab
    anchors.fill: parent
    model: invoice.tabmodel
    Button{
        width: 100
        height: parent.height
        text: test
        //text: display
    }
}

The problem is that when I use display role the text is displayed as 2, but when I use test in qml, the string is displaying correctly

Using test Role

enter image description here

When using the display role

enter image description here

Where does this 2 come from?

Upvotes: 2

Views: 60

Answers (1)

peppe
peppe

Reputation: 22796

display is a property of Button.

When using data coming from a model, always use the model. prefix to disambiguate (model.display).

Upvotes: 6

Related Questions