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
Contributor
Posts: 15
Registered: ‎07-09-2013
My Device: Z10
My Carrier: o2

Delete an element of a list in C++

Hello,

 

I hope this is the right forum for my question. I'm facing the following issue  deleting an element of my ListView from Cascades. This is the code a friend of mine gave me but it does not work Smiley Frustrated

void ApplicationUI::deleteText(QString elementToDelete) {
    for (list<int>::iterator itr = listmodel.begin(); i !=     listmodel.end();) {
    if (itr == elementToDelete)
    listmodel->remove(itr);
    }
} 

 

 

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Delete an element of a list in C++

What does "it does not work" mean, specifically? Is there an error during compilation, at run time, or what?

Also, what DataModel subclass are you using for the list?

Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
New Member
Posts: 1
Registered: ‎07-11-2013
My Device: Developer
My Carrier: Airtel

Re: Delete an element of a list in C++

i couldnt see where are you incrementing the itrator... you will end up in infinite loop..

Contributor
Posts: 15
Registered: ‎07-09-2013
My Device: Z10
My Carrier: o2

Re: Delete an element of a list in C++

@sunilsaluja you are right,

yet I somehow made it work, but now I only can delete the last added element and secondly the list is handled as one whole element, means if I drag, let's say the 3. out of 5 list elements, the whole list is dragged to the "Delete" ActionItem and instead the 3. the last intem is deleted.

How can I solve this? Do I need to create a new list for each item I enter?  

 

Ohh and concerning peter9477 's advice I use the QListDataModel<QString> subclass.

 

applicationui.cpp

void ApplicationUI::deleteText(QString elementToDelete) {
    for (int i = 0; i < listmodel->size(); i++) {
    	if (listmodel->value(i) == elementToDelete)
    		listmodel->removeAt(i);
    }
}

 

main.qml

                    DeleteActionItem {
                                id: deletePositionAction
                                title: qsTr("Delete") + Retranslate.onLanguageChanged
                                ActionBar.placement: ActionBarPlacement.InOverflow
                                onTriggered: {
                                    mainObj.deleteText(textfield.text)
                                }
                            }