0

I have made an application in qml. In the code there is a ListView(id:daylistView). In the ListView there is a Label as an element of it (id:notesLabel):

ListView{
        id:daylistView
        orientation: Qt.Vertical
        model:hourlistModel
        delegate:Item{
             id:hourItem
             property string hourTime:hourweeklistviewLabel
             property string notetaking:notesLabe 
             .............
             MouseArea{
                  anchors.fill:parent
                  onClicked:{
                  windowLoader.active =true
                  daylistView.currentIndex=index

                  }
             }  
             Rectangle{}
             Label{
               id:hourweeklistviewLabel
             }
             Label{
               id:notesLabel                                        
               anchors.left:hourweeklistviewLabel.right
               anchors.leftMargin: 30
               text:""
             }//Label
            }//delegate:Item
           }//ListView

The text of the Label is an input in a TextField (id:title):

Loader {
id:windowLoader
focus: true
active:false
sourceComponent: Window{
    id:inputWin
    title:"Enter Note"
    width:500
    height:300
    visible:true

    onClosing:{
        windowLoader.active=false
        monthofdayCalendar.currentItem.daycalendarAlias.currentItem.dayList.currentIndex = calendarMonth.selectedDate.getDate() === new Date().getDate()
                && calendarMonth.selectedDate.getDay() === new Date().getDay()
                && calendarMonth.selectedDate.getMonth() === new Date().getMonth()?getHour():12


    }
    TextField {
        id:title
        x:50
        y:20
        placeholderText :'Enter Note'
        text:monthofdayCalendar.currentItem.daycalendarAlias.currentItem.dayList.currentItem.notetaking.text
    }
    TextField{
        id:timeDate

        anchors.horizontalCenter: title.horizontalCenter
        anchors.top:title.bottom
        anchors.topMargin:10
        placeholderText :  calendarMonth.selectedDate.getDate() +"-"
               + (calendarMonth.selectedDate.getMonth()+1)+"-"
               + calendarMonth.selectedDate.getFullYear() + " "
               + monthofdayCalendar.currentItem.daycalendarAlias.currentItem.dayList.currentItem.hourTime.text
            }

        Button {
             id: button
             text: qsTr("Add Note")
             anchors.centerIn:parent

             onClicked: {
                   if (title.text !==""){monthofdayCalendar.currentItem.daycalendarAlias.currentItem.dayList.currentItem.notetaking.text= title.text}
                   else{}

                }
            }
        }
    }

The input text is set to the notesLabel.text when I push the "Add Note" button.

The ListModel of the ListViewis :

ListModel{
                              id:hourlistModel
                              Component.onCompleted:{
                                  for (var i = 0; i <25; i++){
                                      append(createListElement())
                                  }
                              }
                                property int h:0
                                function createListElement(){

                                    return {

                                        hour : h++
                                  }
                                }
                            }

My question is how can I save the notesLabel.text and restore it, in pure Qml, when I close and re-open the application.

cgiannakidis
  • 81
  • 1
  • 6
  • You need to save the model data so the model is not just `model:24` but some array of data you read from file and save to file. Consider this https://stackoverflow.com/questions/19244672/parse-xmlhttprequest-to-xmllistmodel mind that instead of URI containing web reference that can be local file too. – Alexander V Oct 08 '21 at 19:30

1 Answers1

0

You give a lot of extra code/information that doesn't seem to be related to your question. All you're asking is how to save/restore data when you close/reopen your app. For that, the simplest solution is to use QML's Settings type. Here is an example of how it might look (I haven't tested this):

Window {
    id: window
    property string notesText: daylistView.currentItem.notesLabel.text

    ListView {
        id: daylistView
        ...
    }

    Settings {
        property alias notesText: window.notesText
    }
}

UPDATE:

You should not really be trying to access the delegate items of your list, but rather the model data. It looks like you're trying to populate the labels in your list directly from the TextField in your popup. That's a really bad way to do it. ListViews can delete/reuse delegate items as you scroll, and if you're not storing your data somewhere outside the delegate, the data could be lost. The text that gets entered in the TextField should be written to your model instead of the Label. And then the Label should use a binding into the model to display the updated text.

The problem of nested lists also goes away if you are correctly using models. The data should always be present no matter what the ListView is doing, so you shouldn't run into null. And then also the Settings object should point to the model data instead of the view as well.

property var myModel: [
    {
        someNumber: 1,
        subModel: [
            { someString: "Hello" },
            { someString: "World" }
        ]
    },
    {
        someNumber: 2,
        subModel: [
            { someString: "ABC" },
            { someString: "123" }
        ]
    }
]
ListView {
    id: outerList
    model: myModel
    delegate: ListView {
        id: innerList
        model: myModel[index].subModel
        delegate: Label {
            text: innerList.model[index].someString
        }
    }
}

To update the text in the model, it might look like this:

myModel[<index of outerList>].subModel[<index of innerList>].someString = "some new text"
JarMan
  • 7,589
  • 1
  • 10
  • 25
  • @JarManOK and if the ListView is nested (there are 2 others ListViews ) how do I express the notesText property without "can not read property of null" error? – cgiannakidis Oct 09 '21 at 04:41
  • 1
    See my edit above. – JarMan Oct 09 '21 at 13:18
  • @JarManI have made some changes to my listmodels and I want to ask if you can be more thorough in how to update the text in the model .Can I use `myModel.currentIndex.subModel.currentIndex.someString= title.text`? – cgiannakidis Oct 09 '21 at 16:49
  • 1
    No, something more like this: `myModel[outerList.currentIndex].subModel[innerList.currentIndex].someString = title.text` – JarMan Oct 09 '21 at 18:09
  • I implemented the above code to my app but I get an error " Cannot read property 'daylistModel' of undefined" .Is it possible to take a look, if you want and have time to help , to my code in below link? https://gist.github.com/cgiannakidis70/fca82174f6db04e444ca07c12cb5d0e7 – cgiannakidis Oct 09 '21 at 19:05
  • Your model is not structured anything like my example. Since you referred to nested listviews, don't you need nested models? – JarMan Oct 09 '21 at 19:48
  • do you mean something like this: https://gist.github.com/cgiannakidis70/12383d2ebab1ad6d25250c6fe2dc7b29 – cgiannakidis Oct 09 '21 at 21:20
  • 1
    Something like that, yes. Although, I don't think you can have multiple items with the same name in a single object. `{ day:0;day:1;day:2 }` – JarMan Oct 09 '21 at 22:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237994/discussion-between-cgiannakidis-and-jarman). – cgiannakidis Oct 09 '21 at 22:17
  • Ok I have made some changes to my code as you can see in the link https://gist.github.com/cgiannakidis70/12383d2ebab1ad6d25250c6fe2dc7b29 but I get error when I run the app expected token '}' in line 824.Any suggestion ? – cgiannakidis Oct 10 '21 at 05:31
  • I tried out my example code and realized I had some typos (missing commas) and I had to access the model a little differently for some reason. What I have there now works. – JarMan Oct 10 '21 at 19:43