Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
New Developer
georg
Posts: 86
Registered: ‎01-23-2009
My Device: Z10

Update Sorting in ListView

I have a ListView with a GroupDataModel (list_items) which sorts the items by one property like this:

 

this->list_items->setSortingKeys(QStringList("SequenceNumber"));

 I want to be able to move some items up and down the list by updating their sequence number. I have defined the property like this:

 

int sequence_number;											
Q_PROPERTY(int SequenceNumber READ getSequenceNumber NOTIFY onSequenceNumberChanged FINAL);

 where I emit the signal on sequence number update.

 

However something is missing, my list stays unchanged. Can someone help?

Please use plain text.
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: Update Sorting in ListView

[ Edited ]

I think you'll have to do a remove/insert to make the model aware of the change.

 

Alternately - and less efficiently - you could conncet your sequence changed signal to a slot which changes and reverts the sorting order.

 

 

--
Rob is no longer associated with BlackBerry.
Please use plain text.
New Developer
georg
Posts: 86
Registered: ‎01-23-2009
My Device: Z10

Re: Update Sorting in ListView

Well remove/insert does not work if the event originates from the affected item rows.
Please use plain text.
Retired
robbieDubya
Posts: 418
Registered: ‎07-18-2012
My Device: Q10

Re: Update Sorting in ListView

[ Edited ]

Hi,

 

I've just built a test to see if I can reproduce this - and it worked for me...

 

Tested on 10.2.0.1743 (Z10)

 

I have an Item that looks like this;

class Item: public QObject {
Q_OBJECT
public:
	Item(QObject * owner = NULL) : QObject(owner), _sequenceNumber(0) {
	}
	virtual ~Item() { }

	Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged);
	Q_INVOKABLE QString name() {
		return _name;
	}
	Q_INVOKABLE void setName(const QString & name) {
		if (_name!=name) {
			_name = name;
			emit nameChanged(name);
		}
	}

	Q_PROPERTY(int sequenceNumber READ sequenceNumber WRITE setSequenceNumber NOTIFY sequenceNumberChanged);
	Q_INVOKABLE int sequenceNumber() {
		return _sequenceNumber;
	}
	Q_INVOKABLE void setSequenceNumber(int sequenceNumber) {
		if (_sequenceNumber!=sequenceNumber) {
			_sequenceNumber = sequenceNumber;
			emit sequenceNumberChanged(sequenceNumber);
		}
	}

signals:
	void nameChanged(QString);
	void sequenceNumberChanged(int);

private:
	QString _name;
	int _sequenceNumber;
};

 Created my data model like this:

	ListView * lv = root->findChild<ListView*>("listView");

	GroupDataModel * gdm = new GroupDataModel(this);

	gdm->setSortingKeys(QStringList() << "sequenceNumber");

	for (int i = 0; i < 10; ++i) {
		Item * next = new Item(this);

		next->setName(QString("Dude %1").arg(i));
		next->setSequenceNumber(rand() % 20);

		items << next;
		gdm->insert(next);
	}

	lv->setDataModel(gdm);

 Added a timer, and tampered with the sequence numbers like this:

void ApplicationUI::onTick() {
	int which = rand() % items.size();
	items[which]->setSequenceNumber(
			items[which]->sequenceNumber() + (rand() % 10 - 8));
}

 Used this QML:

import bb.cascades 1.2

Page {
    Container {
        //Todo: fill me with QML
        ListView {
            objectName: "listView"
            listItemComponents: [
                ListItemComponent {
                    type: "item"
                    content: Container {
                        Label {
                            text: ListItemData.name + "  [" + ListItemData.sequenceNumber + "]"
                        }
                    }
                }
            ]
        }
    }
}

 Can you provide more code?

 

Thanks!

 

 

--
Rob is no longer associated with BlackBerry.
Please use plain text.
New Developer
georg
Posts: 86
Registered: ‎01-23-2009
My Device: Z10

Re: Update Sorting in ListView

It works for me now too. After some further experiments it suddenly started to work. Though now happy with it I still don't know what the problem originally was.
Please use plain text.