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
Regular Contributor
brad_qqq
Posts: 81
Registered: ‎03-27-2013
My Device: Dev Alpha C,Z10 LE
Accepted Solution

How to delete object dynamically?

Hi all,

 

I have dynamically created an object.But I don't know how to dynamically delete it in QML.

I have called "destroy()"function,but I don't know it work or not.The code is as follows:

LogView.qml

import bb.cascades 1.0

ScrollView {
    id:logviewaaa
    
    signal addnewlog
    
    property variant number:100
    property variant log1
    property variant log2
    property variant log3
    property variant log4
    property variant log5
    property variant log6
    property variant log7
    property variant log8
    property variant log9
    property variant log10

    onAddnewlog:{
        
        number=logcontain.count()
        console.log("the log number is "+number)
        if(number == 0)
        {
            console.log("ready to add the first log");
            log1 = log.createObject(logcontain);
            console.log("ready to add the first log");
            logcontain.add(log1);

        }else if(number ==1){

            log2 = log.createObject(logcontain)
            logcontain.add(log2)

        }else if(number ==2){
            log3 =log.createObject(logcontain)
            logcontain.add(log3)
        }else if(number ==3){
            log4=log.createObject(logcontain)
            logcontain.add(log4)
        }else if(number == 4){
            log5=log.createObject(logcontain)
            logcontain.add(log5)
        }else if(number ==5){
            log6=log.createObject(logcontain)
            logcontain.add(log6)
        }else if(number ==6){
            log7=log.createObject(logcontain)
            logcontain.add(log7)
        }else if(number ==7){
            log8=log.createObject(logcontain)
            logcontain.add(log8)
        }else if(number ==8){
            log9=log.createObject(logcontain)
            logcontain.add(log9)
        }else if(number ==10){
            log10=log.createObject(logcontain)
            logcontain.add(log10)
        }
     }
 
    Container{
     id:logcontain      
    
    onCreationCompleted:{
    }
      
      attachedObjects:[
          ComponentDefinition{
              id:log
              source:"Log.qml"
          }
          
      ]
  
    }
 
}

 Log.qml:

import bb.cascades 1.0


    Container {
       id:root
        signal deletethislog
        
        
        TextArea{
            id:recordlog
            inputMode:TextAreaInputMode.Text
        }
    Container {
         id:buttoncontain
        layout: StackLayout {
            orientation: LayoutOrientation.LeftToRight
        }
        Button {
            id: edittext
            imageSource: "asset:///icons/ic_edit.png"
            onClicked: {
                recordlog.editable = true
            }

        }
        Button {
            id: oktext
            imageSource: "asset:///icons/ic_done.png"
            onClicked: {
                recordlog.editable = false
            }

        }
        Button{
            id:deletetext
            imageSource: "asset:///icons/ic_delete.png"
            onClicked: {
                destroy()               
            }
            
            layoutProperties: AbsoluteLayoutProperties {

            }
            horizontalAlignment: HorizontalAlignment.Right

        }
    }
    onCreationCompleted:{
    }
}

 Thanks.

 

brad_qqq

 

Developer
simon_hain
Posts: 16,282
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport

Re: How to delete object dynamically?

i created a Q_INVOKABLE method in an utility class that calls deleteLater on any object given to it.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Regular Contributor
brad_qqq
Posts: 81
Registered: ‎03-27-2013
My Device: Dev Alpha C,Z10 LE

Re: How to delete object dynamically?

@simon_hain-So I need to declare a Q_INVOKABLE method in an class and use it to call deleteLater in QML.I have just sarched Q_INVOKABLE and deleteLater,thinking it will solve my problem.

Regular Contributor
brad_qqq
Posts: 81
Registered: ‎03-27-2013
My Device: Dev Alpha C,Z10 LE

Re: How to delete object dynamically?

I have solved this problem.I still use "destroy()" function to solve it.The code is as follows:

LogView.qml

import bb.cascades 1.0

ScrollView {
    id:logviewaaa
    
    signal addnewlog
    
    property variant number:100
    property variant log1
    property variant log2
    property variant log3
    property variant log4
    property variant log5
    property variant log6
    property variant log7
    property variant log8
    property variant log9
    property variant log10

    onAddnewlog:{
        
        number=logcontain.count()
        console.log("the log number is "+number)
        if(number == 0)
        {
            console.log("ready to add the first log");
            log1 = log.createObject(logcontain);
            console.log("ready to add the first log");
            logcontain.add(log1);
            log1.objectaaa=log1
            log1.maincontain=logcontain

        }else if(number ==1){

            log2 = log.createObject(logcontain)
            logcontain.add(log2)
            log2.objectaaa = log2
            log2.maincontain = logcontain

        }else if(number ==2){
            log3 =log.createObject(logcontain)
            logcontain.add(log3)
            log3.objectaaa = log3
            log3.maincontain = logcontain
        }else if(number ==3){
            log4=log.createObject(logcontain)
            logcontain.add(log4)
            log4.objectaaa = log4
            log4.maincontain = logcontain
        }else if(number == 4){
            log5=log.createObject(logcontain)
            logcontain.add(log5)
            log5.objectaaa = log5
            log5.maincontain = logcontain
        }else if(number ==5){
            log6=log.createObject(logcontain)
            logcontain.add(log6)
            log6.objectaaa = log6
            log6.maincontain = logcontain
        }else if(number ==6){
            log7=log.createObject(logcontain)
            logcontain.add(log7)
            log7.objectaaa = log7
            log7.maincontain = logcontain
        }else if(number ==7){
            log8=log.createObject(logcontain)
            logcontain.add(log8)
            log8.objectaaa = log8
            log8.maincontain = logcontain
        }else if(number ==8){
            log9=log.createObject(logcontain)
            logcontain.add(log9)
            log9.objectaaa = log9
            log9.maincontain = logcontain
        }else if(number ==10){
            log10=log.createObject(logcontain)
            logcontain.add(log10)
            log10.objectaaa = log10
            log10.maincontain = logcontain
        }
     }
 
    Container{
     id:logcontain      
    
    onCreationCompleted:{
    }
      
      attachedObjects:[
          ComponentDefinition{
              id:log
              source:"Log.qml"
          }
          
      ]
  
    }

}

 Log.qml:

import bb.cascades 1.0


    Container {
       id:root
       //objectName:"thislog"
        signal deletethislog
        property variant objectaaa
        property variant maincontain
        
        TextArea{
            id:recordlog
            inputMode:TextAreaInputMode.Text
        }
    Container {
         id:buttoncontain
        layout: StackLayout {
            orientation: LayoutOrientation.LeftToRight
        }
        Button {
            id: edittext
            
            imageSource: "asset:///icons/ic_edit.png"
            onClicked: {
                recordlog.editable = true
            }

        }
        Button {
            id: oktext
            imageSource: "asset:///icons/ic_done.png"
            onClicked: {
                recordlog.editable = false
            }

        }
        Button{
            id:deletetext
            imageSource: "asset:///icons/ic_delete.png"
            onClicked: {
              
               selfdestroy()         
            }
            
            layoutProperties: AbsoluteLayoutProperties {

            }
            horizontalAlignment: HorizontalAlignment.Right

        }
    }
    onCreationCompleted:{
        deletetext.clicked.connect(deletethislog)
    }
    function selfdestroy(){
        objectaaa.destroy()
        maincontain.remove(objectaaa)
        
    }
}

 brad_qqq