stupidstudent
stupidstudent

Reputation: 698

QML Update Gridview from QML or C++ without emit dataChanged

I have a QAbstractListModel, which is tied to QML via

engine.rootContext()->setContextProperty

which is displayed in a GridView. It contains cards like Aces, Queens etc. I would like to sort the cards in different ways (like color, type etc). The sorting fuction can be called via qml:

GridView
{
    id:table_player
    model: Playerfield
    delegate: Card
    {
        card_id: model.card_id
        Component.onCompleted:
        {
            Playerfield.sortDeck()
        }
    }
}

The C++ Code:

public slots:
    Q_INVOKABLE void sortDeck();

It works, but only updates the Playfield, when a card is changed / a new card is played. I need a way to send a signal other than "emit dataChanged()" to QML to update. Or a way directly from QML to Update the Gridview with the changed model data (table_player.update() does not work).

void Deckmodel::sortDeck()
{
    for(uint a = 0; a < cards.size(); a++)
    {
        for(uint b = a+1; b < cards.size(); b++)
        {
            if(cards[a].type > cards[b].type)
            {
                Card temp = cards[a];
                cards[a] = cards[b];
                cards[b] = temp;
            }
        }
    }
//insert signal here
}

Upvotes: 0

Views: 256

Answers (1)

JarMan
JarMan

Reputation: 8277

Is Deckmodel the same thing as Playerfield in your code? When changing your model, you need to call begin/endResetModel(). That will automatically emit the appropriate signals so your QML should update correclty.

void Deckmodel::sortDeck()
{
    beginResetModel();

    for(uint a = 0; a < cards.size(); a++)
    {
        for(uint b = a+1; b < cards.size(); b++)
        {
            if(cards[a].type > cards[b].type)
            {
                Card temp = cards[a];
                cards[a] = cards[b];
                cards[b] = temp;
            }
        }
    }

    endResetModel();
}

Upvotes: 1

Related Questions